Changeset 5874 for branches/cleanup


Ignore:
Timestamp:
02/02/10 04:44:32 (10 years ago)
Author:
callan
Message:
  • Implemented NIOFileHandle ByteBuffer read/write operations.
  • Adjusted ByteArrayHandle ByteBuffer write slightly.
  • Added a couple new test cases for sub-buffer and sub-array multi-byte
Location:
branches/cleanup/components/common
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/cleanup/components/common/src/loci/common/ByteArrayHandle.java

    r5869 r5874  
    150150  } 
    151151 
    152   /* @see IRandomAccess.write(ByteBuffer) */ 
    153   public void write(ByteBuffer buf) throws IOException { 
    154     write(buf, 0, buf.capacity()); 
    155   } 
    156  
    157   /* @see IRandomAccess.write(ByteBuffer, int, int) */ 
    158   public void write(ByteBuffer buf, int off, int len) throws IOException { 
    159     validateLength(len); 
    160  
    161     buf.position(off); 
    162     if (len == buf.remaining()) buffer.put(buf); 
    163     else { 
    164       for (int i=0; i<len; i++) { 
    165         buffer.put(buf.get()); 
    166       } 
    167     } 
    168   } 
    169  
    170152  /* @see IRandomAccess.getOrder() */ 
    171153  public ByteOrder getOrder() { 
     
    352334    validateLength(len); 
    353335    buffer.put(b, off, len); 
     336  } 
     337 
     338  /* @see IRandomAccess.write(ByteBuffer) */ 
     339  public void write(ByteBuffer buf) throws IOException { 
     340    write(buf, 0, buf.capacity()); 
     341  } 
     342 
     343  /* @see IRandomAccess.write(ByteBuffer, int, int) */ 
     344  public void write(ByteBuffer buf, int off, int len) throws IOException { 
     345    validateLength(len); 
     346    buf.position(off); 
     347    buf.limit(off + len); 
     348    buffer.put(buf); 
    354349  } 
    355350 
  • branches/cleanup/components/common/src/loci/common/NIOFileHandle.java

    r5869 r5874  
    162162  /* @see IRandomAccess.read(byte[]) */ 
    163163  public int read(byte[] b) throws IOException { 
    164     return read(b, 0, b.length); 
     164    return read(ByteBuffer.wrap(b)); 
    165165  } 
    166166 
    167167  /* @see IRandomAccess.read(byte[], int, int) */ 
    168168  public int read(byte[] b, int off, int len) throws IOException { 
    169     ByteBuffer destination = ByteBuffer.wrap(b, off, len); 
     169    return read(ByteBuffer.wrap(b), off, len); 
     170  } 
     171 
     172  /* @see IRandomAccess.read(ByteBuffer) */ 
     173  public int read(ByteBuffer buf) throws IOException { 
     174    return read(buf, 0, buf.capacity()); 
     175  } 
     176 
     177  /* @see IRandomAccess.read(ByteBuffer, int, int) */ 
     178  public int read(ByteBuffer buf, int off, int len) throws IOException { 
     179    buf.position(off); 
     180    buf.limit(off + len); 
    170181    channel.position(position); 
    171     int readLength = channel.read(destination); 
     182    int readLength = channel.read(buf); 
    172183    buffer(position + readLength, 0); 
    173184    // Return value of NIO channel's is -1 when zero bytes are read at the end 
     
    176187  } 
    177188 
    178   /* @see IRandomAccess.read(ByteBuffer) */ 
    179   public int read(ByteBuffer buf) throws IOException { 
    180     // TODO 
    181     return 0; 
    182   } 
    183  
    184   /* @see IRandomAccess.read(ByteBuffer, int, int) */ 
    185   public int read(ByteBuffer buf, int off, int len) throws IOException { 
    186     // TODO 
    187     return 0; 
    188   } 
    189  
    190189  /* @see IRandomAccess.seek(long) */ 
    191190  public void seek(long pos) throws IOException { 
    192191    buffer(pos, 0); 
    193192  } 
    194  
    195   /* @see IRandomAccess.write(ByteBuffer) */ 
    196   public void write(ByteBuffer buf) throws IOException { 
    197     // TODO 
    198   } 
    199  
    200   /* @see IRandomAccess.write(ByteBuffer, int, int) */ 
    201   public void write(ByteBuffer buf, int off, int len) throws IOException { 
    202     // TODO 
    203   } 
    204  
    205   // -- DataInput API methods -- 
    206193 
    207194  /* @see java.io.DataInput.readBoolean() */ 
     
    353340  /* @see java.io.DataOutput.write(byte[]) */ 
    354341  public void write(byte[] b) throws IOException { 
    355     validateLength(b.length); 
    356     buffer(position, b.length); 
    357     buffer.put(b); 
    358     position += b.length; 
     342    write(ByteBuffer.wrap(b)); 
    359343  } 
    360344 
    361345  /* @see java.io.DataOutput.write(byte[], int, int) */ 
    362346  public void write(byte[] b, int off, int len) throws IOException { 
     347    write(ByteBuffer.wrap(b), off, len); 
     348  } 
     349 
     350  /* @see IRandomAccess.write(ByteBuffer) */ 
     351  public void write(ByteBuffer buf) throws IOException { 
     352    write(buf, 0, buf.capacity()); 
     353  } 
     354 
     355  /* @see IRandomAccess.write(ByteBuffer, int, int) */ 
     356  public void write(ByteBuffer buf, int off, int len) throws IOException { 
    363357    validateLength(len); 
    364     buffer(position, len - off); 
    365     buffer.put(ByteBuffer.wrap(b, off, len)); 
    366     position += len; 
     358    buf.position(off); 
     359    buf.limit(off + len); 
     360    channel.position(position); 
     361    buffer(position + channel.write(buf), 0); 
    367362  } 
    368363 
  • branches/cleanup/components/common/test/loci/common/utests/WriteByteArrayTest.java

    r5836 r5874  
    135135    assertEquals(1, fileHandle.readByte()); 
    136136  } 
     137   
     138  @Test 
     139  public void testWriteTwoByteSubArray() throws IOException { 
     140    byte[] buf = new byte[] { 1, 2, 3, 4 }; 
     141    fileHandle.write(buf, 1, 2); 
     142    assertEquals(2, fileHandle.getFilePointer()); 
     143    fileHandle.seek(0); 
     144    assertEquals(2, fileHandle.readByte()); 
     145    assertEquals(3, fileHandle.readByte()); 
     146    assertEquals(0, fileHandle.readByte()); 
     147  } 
    137148 
    138149  @Test 
  • branches/cleanup/components/common/test/loci/common/utests/WriteByteBufferTest.java

    r5870 r5874  
    147147    assertEquals(1, fileHandle.readByte()); 
    148148  } 
     149   
     150  @Test 
     151  public void testWriteTwoByteSubBuffer() throws IOException { 
     152    ByteBuffer b = ByteBuffer.allocate(4); 
     153    b.put((byte) 1); 
     154    b.put((byte) 2); 
     155    b.put((byte) 3); 
     156    b.put((byte) 4); 
     157    fileHandle.write(b, 1, 2); 
     158    assertEquals(2, fileHandle.getFilePointer()); 
     159    fileHandle.seek(0); 
     160    assertEquals(2, fileHandle.readByte()); 
     161    assertEquals(3, fileHandle.readByte()); 
     162    assertEquals(0, fileHandle.readByte()); 
     163  } 
    149164 
    150165  @Test 
Note: See TracChangeset for help on using the changeset viewer.