Changeset 4917


Ignore:
Timestamp:
03/16/09 11:21:22 (11 years ago)
Author:
melissa
Message:

Fixed several problems with writing to an RABytes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/common/src/loci/common/RABytes.java

    r4535 r4917  
    3939public class RABytes implements IRandomAccess { 
    4040 
     41  // -- Constants -- 
     42 
     43  /** Initial length of a new file. */ 
     44  protected static final int INITIAL_LENGTH = 1000000; 
     45 
    4146  // -- Fields -- 
    4247 
     
    4651  /** The file pointer. */ 
    4752  protected int fp; 
     53 
     54  /** File length. */ 
     55  protected long length; 
    4856 
    4957  // -- Constructors -- 
     
    5664    array = bytes; 
    5765    fp = 0; 
     66    length = bytes.length; 
     67  } 
     68 
     69  /** Creates a random access byte stream to write to a byte array. */ 
     70  public RABytes() { 
     71    fp = 0; 
     72    length = 0; 
     73    array = new byte[INITIAL_LENGTH]; 
    5874  } 
    5975 
     
    6177 
    6278  /** Gets the byte array backing this RAFile. */ 
    63   public byte[] getBytes() { return array; } 
     79  public byte[] getBytes() { 
     80    byte[] file = new byte[(int) length]; 
     81    System.arraycopy(array, 0, file, 0, file.length); 
     82    return file; 
     83  } 
    6484 
    6585  // -- IRandomAccess API methods -- 
     
    7595  /* @see IRandomAccess.length() */ 
    7696  public long length() { 
    77     return array.length; 
     97    return length; 
    7898  } 
    7999 
    80100  /* @see IRandomAccess.read() */ 
    81101  public int read() { 
    82     return fp < array.length ? array[fp++] : 0; 
     102    return fp < length() ? array[fp++] : 0; 
    83103  } 
    84104 
     
    90110  /* @see IRandomAccess.read(byte[], int, int) */ 
    91111  public int read(byte[] b, int off, int len) throws IOException { 
    92     if (fp + len > array.length) len = array.length - fp; 
     112    if (fp + len > length()) len = (int) (length() - fp); 
    93113    if (len > 0) System.arraycopy(array, fp, b, off, len); 
    94114    fp += len; 
     
    108128      throw new IOException("newLength is too large"); 
    109129    } 
    110     int len = (int) newLength; 
    111     byte[] bytes = new byte[len]; 
     130 
     131    length = newLength; 
     132    if (length < array.length) return; 
     133 
     134    byte[] bytes = new byte[(int) (length * 2)]; 
    112135    System.arraycopy(array, 0, bytes, 0, 
    113       array.length < len ? array.length : len); 
     136      array.length < bytes.length ? array.length : bytes.length); 
    114137    array = bytes; 
    115138  } 
     
    124147  /* @see java.io.DataInput.readByte() */ 
    125148  public byte readByte() throws IOException { 
    126     if (fp + 1 > array.length) throw new EOFException(); 
     149    if (fp + 1 > length()) throw new EOFException(); 
    127150    return array[fp++]; 
    128151  } 
     
    130153  /* @see java.io.DataInput.readChar() */ 
    131154  public char readChar() throws IOException { 
    132     if (fp + 2 > array.length) throw new EOFException(); 
     155    if (fp + 2 > length()) throw new EOFException(); 
    133156    char c = (char) DataTools.bytesToShort(array, fp, false); 
    134157    fp += 2; 
     
    138161  /* @see java.io.DataInput.readDouble() */ 
    139162  public double readDouble() throws IOException { 
    140     if (fp + 8 > array.length) throw new EOFException(); 
     163    if (fp + 8 > length()) throw new EOFException(); 
    141164    double d = Double.longBitsToDouble(DataTools.bytesToLong(array, fp, false)); 
    142165    fp += 8; 
     
    146169  /* @see java.io.DataInput.readFloat() */ 
    147170  public float readFloat() throws IOException { 
    148     if (fp + 4 > array.length) throw new EOFException(); 
     171    if (fp + 4 > length()) throw new EOFException(); 
    149172    float f = Float.intBitsToFloat(DataTools.bytesToInt(array, fp, false)); 
    150173    fp += 4; 
     
    159182  /* @see java.io.DataInput.readFully(byte[], int, int) */ 
    160183  public void readFully(byte[] b, int off, int len) throws IOException { 
    161     if (fp + len > array.length) throw new EOFException(); 
     184    if (fp + len > length()) throw new EOFException(); 
    162185    System.arraycopy(array, fp, b, off, len); 
    163186    fp += len; 
     
    166189  /* @see java.io.DataInput.readInt() */ 
    167190  public int readInt() throws IOException { 
    168     if (fp + 4 > array.length) throw new EOFException(); 
     191    if (fp + 4 > length()) throw new EOFException(); 
    169192    int i = DataTools.bytesToInt(array, fp, false); 
    170193    fp += 4; 
     
    179202  /* @see java.io.DataInput.readLong() */ 
    180203  public long readLong() throws IOException { 
    181     if (fp + 8 > array.length) throw new EOFException(); 
     204    if (fp + 8 > length()) throw new EOFException(); 
    182205    long l = DataTools.bytesToLong(array, fp, false); 
    183206    fp += 8; 
     
    187210  /* @see java.io.DataInput.readShort() */ 
    188211  public short readShort() throws IOException { 
    189     if (fp + 2 > array.length) throw new EOFException(); 
     212    if (fp + 2 > length()) throw new EOFException(); 
    190213    short s = DataTools.bytesToShort(array, fp, false); 
    191214    fp += 2; 
     
    195218  /* @see java.io.DataInput.readUnsignedByte() */ 
    196219  public int readUnsignedByte() throws IOException { 
    197     if (fp + 1 > array.length) throw new EOFException(); 
     220    if (fp + 1 > length()) throw new EOFException(); 
    198221    return DataTools.bytesToInt(array, fp++, 1, false); 
    199222  } 
     
    201224  /* @see java.io.DataInput.readUnsignedShort() */ 
    202225  public int readUnsignedShort() throws IOException { 
    203     if (fp + 2 > array.length) throw new EOFException(); 
     226    if (fp + 2 > length()) throw new EOFException(); 
    204227    int i = DataTools.bytesToInt(array, fp, 2, false); 
    205228    fp += 2; 
     
    215238  public int skipBytes(int n) { 
    216239    if (n < 0) n = 0; 
    217     if (fp + n > array.length) n = array.length - fp; 
     240    if (fp + n > length()) n = (int) (length() - fp); 
    218241    fp += n; 
    219242    return n; 
     
    229252  /* @see java.io.DataOutput.write(byte[], int, int) */ 
    230253  public void write(byte[] b, int off, int len) throws IOException { 
    231     if (fp + len > array.length) setLength(fp + len); 
     254    if (fp + len > length()) setLength(fp + len); 
    232255    System.arraycopy(b, off, array, fp, len); 
    233256    fp += b.length; 
     
    236259  /* @see java.io.DataOutput.write(int b) */ 
    237260  public void write(int b) throws IOException { 
    238     if (fp + 1 > array.length) setLength(fp + 1); 
     261    if (fp + 1 > length()) setLength(fp + 1); 
    239262    array[fp++] = (byte) b; 
    240263  } 
     
    260283  /* @see java.io.DataOutput.writeChar(int) */ 
    261284  public void writeChar(int v) throws IOException { 
    262     if (fp + 2 > array.length) setLength(fp + 2); 
     285    if (fp + 2 > length()) setLength(fp + 2); 
    263286    array[fp++] = (byte) (0xff & (v >> 8)); 
    264287    array[fp++] = (byte) (0xff & v); 
     
    268291  public void writeChars(String s) throws IOException { 
    269292    int len = 2 * s.length(); 
    270     if (fp + len > array.length) setLength(fp + len); 
     293    if (fp + len > length()) setLength(fp + len); 
    271294    char[] c = s.toCharArray(); 
    272295    for (int i=0; i<c.length; i++) { 
     
    289312  /* @see java.io.DataOutput.writeInt(int) */ 
    290313  public void writeInt(int v) throws IOException { 
    291     if (fp + 4 > array.length) setLength(fp + 4); 
    292     array[fp++] = (byte) (0xff & (v >> 24)); 
    293     array[fp++] = (byte) (0xff & (v >> 16)); 
    294     array[fp++] = (byte) (0xff & (v >> 8)); 
    295     array[fp++] = (byte) (0xff & v); 
     314    if (fp + 4 > length()) setLength(fp + 4); 
     315    for (int i=0; i<4; i++) { 
     316      array[fp++] = (byte) (0xff & (v >> ((3 - i) * 8))); 
     317    } 
    296318  } 
    297319 
    298320  /* @see java.io.DataOutput.writeLong(long) */ 
    299321  public void writeLong(long v) throws IOException { 
    300     if (fp + 8 > array.length) setLength(fp + 8); 
    301     array[fp++] = (byte) (0xff & (v >> 56)); 
    302     array[fp++] = (byte) (0xff & (v >> 48)); 
    303     array[fp++] = (byte) (0xff & (v >> 40)); 
    304     array[fp++] = (byte) (0xff & (v >> 32)); 
    305     array[fp++] = (byte) (0xff & (v >> 24)); 
    306     array[fp++] = (byte) (0xff & (v >> 16)); 
    307     array[fp++] = (byte) (0xff & (v >> 8)); 
    308     array[fp++] = (byte) (0xff & v); 
     322    if (fp + 8 > length()) setLength(fp + 8); 
     323    for (int i=0; i<8; i++) { 
     324      array[fp++] = (byte) (0xff & (v >> ((7 - i) * 8))); 
     325    } 
    309326  } 
    310327 
    311328  /* @see java.io.DataOutput.writeShort(int) */ 
    312329  public void writeShort(int v) throws IOException { 
    313     if (fp + 2 > array.length) setLength(fp + 2); 
    314     array[fp++] = (byte) (0xff & (v >> 24)); 
    315     array[fp++] = (byte) (0xff & (v >> 16)); 
     330    if (fp + 2 > length()) setLength(fp + 2); 
    316331    array[fp++] = (byte) (0xff & (v >> 8)); 
    317332    array[fp++] = (byte) (0xff & v); 
Note: See TracChangeset for help on using the changeset viewer.