@Override public int read(byte[] buf, int offset, int length) throws IOException { if(rafOffset >= rafLength) throw new EOFException(); length = (int) Math.min((long)length, rafLength-rafOffset); underlying.pread(rafOffset, buf, offset, length); rafOffset += length; return length; }
public byte[] getBlockData(int blockNum) { long fileOffset = (long)blockNum * (long)blockSize; int bs = (int) Math.min(blockSize, size - fileOffset); byte[] data = new byte[bs]; try { raf.pread(fileOffset, data, 0, bs); } catch (IOException e) { Logger.error(this, "Failed to read stored block "+blockNum+" on "+this+" : "+e, e); abort(RetrievalException.IO_ERROR, e.toString()); return null; } return data; }
private void readWriteMustSucceed(RandomAccessBuffer raf, long startAt, byte[] buf, int offset, int length) throws IOException { raf.pread(startAt, buf, 0, buf.length); // Should work raf.pwrite(startAt, buf, 0, buf.length); // Should work }
/** Check that the array section equals the read data, then write it and repeat the check. */ public static void checkArraySectionEqualsReadData(byte[] buf, RandomAccessBuffer raf, int start, int end, boolean readOnly) throws IOException { int len = end - start; if(len == 0) return; byte[] tmp = new byte[len]; raf.pread(start, tmp, 0, len); for(int i=0;i<len;i++) assertEquals(tmp[i], buf[start+i]); if(!readOnly) { raf.pwrite(start, buf, start, len); } for(int i=0;i<len;i++) tmp[i] = 0; raf.pread(start, tmp, 0, len); for(int i=0;i<len;i++) assertEquals(tmp[i], buf[start+i]); }
private void readWriteMustFail(RandomAccessBuffer raf, long startAt, byte[] buf, int offset, int length) throws IOException { if(length == 0) return; // NOP. try { raf.pread(startAt, buf, 0, buf.length); // Should work fail("Must throw!"); } catch (IOException e) { // Ok. } try { raf.pwrite(startAt, buf, 0, buf.length); // Should work fail("Must throw!"); } catch (IOException e) { // Ok. } }
raf.pwrite(x, buf, 0, toRead); for(int i=0;i<buf.length;i++) buf[i] = (byte)~buf[i]; raf.pread(x, buf, 0, toRead); for(int i=0;i<buf.length;i++) assertEquals(buf[i], f.getByte(i+x)); x += toRead; int toRead = r.nextInt(maxRead-1)+1; byte[] buf = new byte[toRead]; raf.pread(x, buf, 0, toRead); for(int i=0;i<buf.length;i++) assertEquals(buf[i], f.getByte(i+x)); x += toRead;