Changeset 5869 for branches/cleanup


Ignore:
Timestamp:
02/01/10 10:50:09 (10 years ago)
Author:
melissa
Message:

Added methods for reading to and writing from a ByteBuffer.

Location:
branches/cleanup/components/common/src/loci/common
Files:
5 edited

Legend:

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

    r5861 r5869  
    127127  } 
    128128 
     129  /* @see IRandomAccess.read(ByteBuffer) */ 
     130  public int read(ByteBuffer buf) throws IOException { 
     131    return read(buf, 0, buf.capacity()); 
     132  } 
     133 
     134  /* @see IRandomAccess.read(ByteBuffer, int, int) */ 
     135  public int read(ByteBuffer buf, int off, int len) throws IOException { 
     136    if (buf.hasArray()) { 
     137      buffer.get(buf.array(), off, len); 
     138      return len; 
     139    } 
     140 
     141    byte[] b = new byte[len]; 
     142    read(b); 
     143    buf.put(b, 0, len); 
     144    return len; 
     145  } 
     146 
    129147  /* @see IRandomAccess.seek(long) */ 
    130148  public void seek(long pos) throws IOException { 
    131149    buffer.position((int) pos); 
     150  } 
     151 
     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    } 
    132168  } 
    133169 
  • branches/cleanup/components/common/src/loci/common/FileHandle.java

    r5790 r5869  
    2828import java.io.IOException; 
    2929import java.io.RandomAccessFile; 
     30import java.nio.ByteBuffer; 
    3031import java.nio.ByteOrder; 
    3132 
     
    99100  } 
    100101 
     102  /* @see IRandomAccess.read(ByteBuffer) */ 
     103  public int read(ByteBuffer buffer) throws IOException { 
     104    return read(buffer, 0, buffer.capacity()); 
     105  } 
     106 
     107  /* @see IRandomAccess.read(ByteBuffer, int, int) */ 
     108  public int read(ByteBuffer buffer, int off, int len) throws IOException { 
     109    byte[] b = new byte[len]; 
     110    int n = read(b); 
     111    buffer.put(b, off, len); 
     112    return n; 
     113  } 
     114 
    101115  /* @see IRandomAccess.seek(long) */ 
    102116  public void seek(long pos) throws IOException { 
     
    104118  } 
    105119 
     120  /* @see IRandomAccess.write(ByteBuffer) */ 
     121  public void write(ByteBuffer buf) throws IOException { 
     122    write(buf, 0, buf.capacity()); 
     123  } 
     124 
     125  /* @see IRandomAccess.write(ByteBuffer, int, int) */ 
     126  public void write(ByteBuffer buf, int off, int len) throws IOException { 
     127    // TODO 
     128  } 
     129 
    106130  // -- DataInput API methods -- 
    107131 
  • branches/cleanup/components/common/src/loci/common/IRandomAccess.java

    r5790 r5869  
    2727import java.io.DataOutput; 
    2828import java.io.IOException; 
     29import java.nio.ByteBuffer; 
    2930import java.nio.ByteOrder; 
    3031 
     
    8081 
    8182  /** 
     83   * Reads up to buffer.capacity() bytes of data 
     84   * from this stream into a ByteBuffer. 
     85   */ 
     86  int read(ByteBuffer buffer) throws IOException; 
     87 
     88  /** 
     89   * Reads up to len bytes of data from this stream into a ByteBuffer. 
     90   * 
     91   * @return the total number of bytes read into the buffer. 
     92   */ 
     93  int read(ByteBuffer buffer, int offset, int len) throws IOException; 
     94 
     95  /** 
    8296   * Sets the stream pointer offset, measured from the beginning 
    8397   * of this stream, at which the next read or write occurs. 
     
    8599  void seek(long pos) throws IOException; 
    86100 
     101  /** 
     102   * Writes up to buffer.capacity() bytes of data from the given 
     103   * ByteBuffer to this stream. 
     104   */ 
     105  void write(ByteBuffer buf) throws IOException; 
     106 
     107  /** 
     108   * Writes up to len bytes of data from the given ByteBuffer to this 
     109   * stream. 
     110   */ 
     111  void write(ByteBuffer buf, int off, int len) throws IOException; 
     112 
    87113} 
  • branches/cleanup/components/common/src/loci/common/NIOFileHandle.java

    r5859 r5869  
    176176  } 
    177177 
     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 
    178190  /* @see IRandomAccess.seek(long) */ 
    179191  public void seek(long pos) throws IOException { 
    180192    buffer(pos, 0); 
     193  } 
     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 
    181203  } 
    182204 
  • branches/cleanup/components/common/src/loci/common/StreamHandle.java

    r5839 r5869  
    2929import java.io.IOException; 
    3030 
     31import java.nio.ByteBuffer; 
    3132import java.nio.ByteOrder; 
    3233 
     
    121122  } 
    122123 
     124  /* @see IRandomAccess#read(ByteBuffer) */ 
     125  public int read(ByteBuffer buffer) throws IOException { 
     126    return read(buffer, 0, buffer.capacity()); 
     127  } 
     128 
     129  /* @see IRandomAccess#read(ByteBuffer, int, int) */ 
     130  public int read(ByteBuffer buffer, int off, int len) throws IOException { 
     131    if (buffer.hasArray()) { 
     132      return read(buffer.array(), off, len); 
     133    } 
     134 
     135    byte[] b = new byte[len]; 
     136    int n = read(b); 
     137    buffer.put(b, off, len); 
     138    return n; 
     139  } 
     140 
    123141  /* @see IRandomAccess#seek(long) */ 
    124142  public void seek(long pos) throws IOException { 
     
    135153      if (n == 0) break; 
    136154      skipped += n; 
     155    } 
     156  } 
     157 
     158  /* @see IRandomAccess.write(ByteBuffer) */ 
     159  public void write(ByteBuffer buf) throws IOException { 
     160    write(buf, 0, buf.capacity()); 
     161  } 
     162 
     163  /* @see IRandomAccess.write(ByteBuffer, int, int) */ 
     164  public void write(ByteBuffer buf, int off, int len) throws IOException { 
     165    buf.position(off); 
     166    if (buf.hasArray()) { 
     167      write(buf.array(), off, len); 
     168    } 
     169    else { 
     170      byte[] b = new byte[len]; 
     171      buf.get(b); 
     172      write(b); 
    137173    } 
    138174  } 
Note: See TracChangeset for help on using the changeset viewer.