/** @deprecated Only for unit tests */ @Deprecated public static void fill(RandomAccessBuffer raf, Random random, long offset, long length) throws IOException { long moved = 0; byte[] buf = new byte[BUFFER_SIZE]; while(moved < length) { int toRead = (int)Math.min(BUFFER_SIZE, length - moved); random.nextBytes(buf); raf.pwrite(offset + moved, buf, 0, toRead); moved += toRead; } }
throw ioException; raf.pwrite(fileOffset, buf, 0, bytes); moved += bytes; fileOffset += bytes;
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 }
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(fileOffset, data, offset, bs); } catch (Throwable t) { Logger.error(this, "Failed to store received block "+blockNum+" on "+this+" : "+t, t);
/** Create an array, fill it with random numbers, write it sequentially to the * RandomAccessBuffer, then read randomly and compare. */ protected void innerTestArray(int len, Random r, boolean readOnly) throws IOException { if(len == 0) return; byte[] buf = new byte[len]; r.nextBytes(buf); RandomAccessBuffer raf = construct(len); raf.pwrite(0L, buf, 0, buf.length); for(int i=0;i<100;i++) { int end = len == 1 ? 1 : r.nextInt(len)+1; int start = r.nextInt(end); checkArraySectionEqualsReadData(buf, raf, start, end, readOnly); } checkArraySectionEqualsReadData(buf, raf, 0, len, readOnly); if(len > 1) checkArraySectionEqualsReadData(buf, raf, 1, len-1, readOnly); raf.close(); raf.free(); }
/** 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]); }
byte[] buf = new byte[toRead]; for(int i=0;i<buf.length;i++) buf[i] = f.getByte(i+x); 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);
/** Create an array, fill it with random numbers, write it sequentially to the * RandomAccessBuffer, then read randomly and compare. */ protected void innerTestArrayMigration(int len, Random r) throws IOException { if(len == 0) return; byte[] buf = new byte[len]; r.nextBytes(buf); RandomAccessBuffer raf = construct(len); TempRandomAccessBuffer t = (TempRandomAccessBuffer) raf; assertFalse(t.hasMigrated()); assertEquals(factory.getRamUsed(), len); t.migrateToDisk(); assertTrue(t.hasMigrated()); assertEquals(factory.getRamUsed(), 0); raf.pwrite(0L, buf, 0, buf.length); checkArrayInner(buf, raf, len, r); raf.close(); raf.free(); }