Package abstrasy

Source Code of abstrasy.Bytes

package abstrasy;


import abstrasy.externals.AExtClonable;

import abstrasy.interpreter.InterpreterException;
import abstrasy.interpreter.StdErrors;

import java.io.UnsupportedEncodingException;


public class Bytes implements AExtClonable{

    byte[] array;
   
    public Bytes(int size) throws InterpreterException {
        if (size<0)
            throw new InterpreterException(StdErrors.extend(StdErrors.Out_of_range, size + " must be >= 0"));
        array=new byte[size];
    }

    public Bytes(String str) throws UnsupportedEncodingException {
        array=str.getBytes("utf-8");
    }
   
    public Bytes(byte[] array){
        this.array=new byte[array.length];
        if(array.length>0)
            System.arraycopy(array, 0, this.array, 0, array.length);
    }
   
    public int length(){
        return array.length;
    }
           
    private final void checkAdr(int adr) throws InterpreterException {
       if (adr<0 || adr >= array.length)
            throw new InterpreterException(StdErrors.extend(StdErrors.Out_of_range, adr + " not in [0.." + array.length + "["));
    }
   
   
    @Override
    public Object clone_my_self(Bivalence bival) throws Exception {
        return copy_my_self();
    }
   
    public Bytes copy_my_self() throws Exception {
        Bytes nb=new Bytes(array.length);
        if (array.length > 0)
            System.arraycopy(array, 0, nb.array, 0, array.length);
        return nb;
    }
   
    public Bytes reverseOf() throws Exception {
        Bytes nb=new Bytes(array.length);
        if (array.length > 0){
            int j=0;
            for(int i=array.length-1;i>=0;)
                nb.array[j++]=array[i--];
        }
        return nb;
    }
    
    //
    // byte
    //
   
    public byte peek(int adr) throws InterpreterException {
        checkAdr(adr);
        return array[adr];
    }

    public void poke(int adr, byte octet) throws InterpreterException {
        checkAdr(adr);
        array[adr] = octet;
    }
   
    //
    // short
    //
   
    public void poke_short(int adr, short v) throws InterpreterException {
        int pc = adr;
        poke(pc++, (byte) ((v >>> 8) & 0xFF));
        poke(pc++, (byte) (v & 0xFF));
    }

    public short peek_short(int adr) throws InterpreterException {
        int pc = adr;
        int v1 = ((int) peek(pc++)) & 0xFF;
        int v2 = ((int) peek(pc)) & 0xFF;
        return (short) ((v1 << 8) + v2);
    }

    //
    // ushort
    //
   
    public void poke_ushort(int adr, int v) throws InterpreterException {
        int pc = adr;
        poke(pc++, (byte) ((v >>> 8) & 0xFF));
        poke(pc++, (byte) (v & 0xFF));
    }

    public int peek_ushort(int adr) throws InterpreterException {
        int pc = adr;
        int v1 = ((int) peek(pc++)) & 0xFF;
        int v2 = ((int) peek(pc)) & 0xFF;
        return ((v1 << 8) + v2);
    }
   
    //
    // int
    //
   
        public void poke_int(int adr, int v) throws InterpreterException {
        int pc = adr;
        poke(pc++, (byte) ((v >>> 24) & 0xFF));
        poke(pc++, (byte) ((v >>> 16) & 0xFF));
        poke(pc++, (byte) ((v >>> 8) & 0xFF));
        poke(pc, (byte) (v & 0xFF));
    }

    public int peek_int(int adr) throws InterpreterException {
        int pc = adr;
        int v1 = ((int) peek(pc++)) & 0xFF;
        int v2 = ((int) peek(pc++)) & 0xFF;
        int v3 = ((int) peek(pc++)) & 0xFF;
        int v4 = ((int) peek(pc++)) & 0xFF;
        return (v1 << 24) | (v2 << 16) | (v3 << 8) | v4;
    }

    //
    // long
    //
   
    public void poke_long(int adr, long v) throws InterpreterException {
        int pc = adr;
        poke(pc++, (byte) ((int) (v >>> 56) & 0xFF));
        poke(pc++, (byte) ((int) (v >>> 48) & 0xFF));
        poke(pc++, (byte) ((int) (v >>> 40) & 0xFF));
        poke(pc++, (byte) ((int) (v >>> 32) & 0xFF));
        poke(pc++, (byte) ((int) (v >>> 24) & 0xFF));
        poke(pc++, (byte) ((int) (v >>> 16) & 0xFF));
        poke(pc++, (byte) ((int) (v >>> 8) & 0xFF));
        poke(pc, (byte) ((int) v & 0xFF));
    }

    public long peek_long(int adr) throws InterpreterException {
        return ((long) (peek_int(adr)) << 32) + (peek_int(adr + 4) & 0xFFFFFFFFL);
    }
   
    //
    // float
    //
   
    public void poke_float(int adr, float v) throws InterpreterException {
        poke_int(adr, Float.floatToIntBits(v));
    }

    public float peek_float(int adr) throws InterpreterException {
        return Float.intBitsToFloat(peek_int(adr));
    }
   
    //
    // double
    //

    public void poke_double(int adr, double v) throws InterpreterException {
        poke_long(adr, Double.doubleToLongBits(v));
    }

    public double peek_double(int adr) throws InterpreterException {
        return Double.longBitsToDouble(peek_long(adr));
    }
   
    //
    // bytes
    //
   
    public void poke_bytes(int adr, Bytes v) throws InterpreterException {
        checkAdr(adr);
        checkAdr(adr+Math.max(v.array.length,1)-1);
        System.arraycopy(v.array, 0, array, adr, v.array.length);
    }

    public Bytes peek_bytes(int adr, int size) throws InterpreterException {
        checkAdr(adr);
        checkAdr(adr+Math.max(size,1)-1);
        Bytes nb=new Bytes(size);
        if (size > 0)
            System.arraycopy(array, adr, nb.array, 0, size);
        return nb;
    }
   
    //
    // string (utf-8)
    //

    public void poke_string(int adr, String v) throws InterpreterException, UnsupportedEncodingException {
        byte[] bytes=v.getBytes("utf-8");
        checkAdr(adr);
        checkAdr(adr+Math.max(bytes.length,1)-1);
        System.arraycopy(bytes, 0, array, adr, bytes.length);
    }

    public String peek_string(int adr, int size) throws InterpreterException, UnsupportedEncodingException {
        checkAdr(adr);
        checkAdr(adr+Math.max(size,1)-1);
        byte[] nb=new byte[size];
        if (size > 0)
            System.arraycopy(array, adr, nb, 0, size);
        return new String(nb,"utf-8");
    }

    public byte[] getArray() {
        byte[] na = new byte[length()];
        System.arraycopy(array, 0, na, 0, array.length);
        return na;
    }

}
TOP

Related Classes of abstrasy.Bytes

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.