Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

ByteArray.java

00001 package gov.nasa.arc.ase.jpf;
00002 
00003 public class ByteArray {
00004   private byte[] data;
00005   private int offset;
00006 
00007   public ByteArray() {
00008     data = null;
00009     offset = 0;
00010   }  
00011   public ByteArray(byte[] d) {
00012     data = d;
00013     offset = 0;
00014   }  
00015   public static byte byteOf(int i, int b) {
00016     return (byte)(i / (1 << (b*8)));
00017   }  
00018   public static int byteOf(int i, int b, byte v) {
00019     int vp = v;
00020 
00021     if(v < 0) v += 256;
00022 
00023     return (i & ~(255 << (b*8))) | (vp << (b*8));
00024   }  
00025   public static byte byteOf(long l, int b) {
00026     return (byte)(l / (1L << (b*8)));
00027   }  
00028   public static long byteOf(long l, int b, byte v) {
00029     long vp = v;
00030 
00031     if(v < 0) v += 256;
00032 
00033     return (l & ~(255L << (b*8))) | (vp << (b*8));
00034   }  
00035   public byte get() {
00036     return data[offset++];
00037   }  
00038   public byte[] getData() {
00039     resize(offset);
00040     return data;
00041   }  
00042   public void pack(int i) {
00043     for(int j = 0; j < 4; j++)
00044       put((byte)((i >> (j*8)) & 0xFF));
00045   }  
00046   public void pack(long l) {
00047     for(int i = 0; i < 8; i++) {
00048       put((byte)((l >> (i*8)) & 0xFF));
00049     }
00050   }  
00051   public void pack(String s) {
00052     int l = s.length();
00053 
00054     put((byte)(l));
00055 
00056     for(int i = 0; i < l; i++)
00057       put((byte)s.charAt(i));
00058   }  
00059   public void pack(boolean b) {
00060     put((byte)(b ? 1 : 0));
00061   }  
00062   public void put(byte b) {
00063     while(true) {
00064       try {
00065     data[offset] = b;
00066     offset++;
00067     return;
00068       } catch(NullPointerException e) {
00069     resize(offset + 4);
00070       } catch(ArrayIndexOutOfBoundsException e) {
00071     resize(offset + 4);
00072       }
00073     }
00074   }  
00075   private void resize(int length) {
00076     if(data == null) {
00077       data = new byte[length];
00078     } else {
00079       byte[] d = new byte[length];
00080 
00081       System.arraycopy(data, 0, d, 0, Math.min(length, data.length));
00082       data = d;
00083     }
00084   }  
00085   public boolean unpackBoolean() {
00086     return get() != 0;
00087   }  
00088   public int unpackInt() {
00089     int i = 0;
00090    
00091     for(int j = 0; j < 4; j++)
00092       i |= (get() << (j*8)) & (0xFFL << (j*8));
00093 
00094     return i;
00095   }  
00096   public long unpackLong() {
00097     long l = 0;
00098    
00099     for(int i = 0; i < 8; i++)
00100       l |= (get() << (i*8)) & (0xFFL << (i*8));
00101 
00102     return l;
00103   }  
00104   public String unpackStr() {
00105     StringBuffer sb = new StringBuffer();
00106     int l = get();
00107 
00108     for(int i = 0; i < l; i++)
00109       sb.append((char)get());
00110 
00111     return sb.toString();
00112   }  
00113 }

Generated at Thu Feb 7 06:41:36 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001