arrays - ByteBuffer in java returning data when nothing is written to it, doesn't throw exception -


i have wrapper around bytebuffer class (because in code, underlying structure entity). want bytebuffer store fixed sized entries in , return null or throw exception if try read @ offset nothing written. i've written following code:

private static final int size = 16; //bytes private static final int bbsize = 48 * size; bytebuffer blockmap = bytebuffer.allocatedirect(bbsize);  byte[] readatoffset(final int offset) throws bufferunderflowexception,                                                indexoutofboundsexception {     byte[] dataread = new byte[size];     blockmap.position(offset);     blockmap.get(dataread);     return dataread; }  void writeatoffset(final int offset, final byte[] data)         throws bufferoverflowexception, indexoutofboundsexception, readonlybufferexception      {      if (data.length != size) {          throw new illegalargumentexception("invalid data received");      }      blockmap.position(offset);      blockmap.put(data); }   public static void main(string[] args) {     bytebuffertests tests = new bytebuffertests();     system.out.println("at 0: " + tests.readatoffset(0));        } 

shouldn't throw exception haven't written buffer yet? doing wrong?

when create bytebuffer full of zeros. full of size create for. if want track portions have written to, have additionally, suggest using index number instead of raw offset, , can use bitset see portions written to. alternative make assumption message won't start nul bytes , if does, corrupt/not present.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -