public void testSmallObjectLoader() throws MissingObjectException, IOException { final byte[] act = rng.nextBytes(512); final ObjectLoader ldr = new ObjectLoader.SmallObject(OBJ_BLOB, act); assertEquals(OBJ_BLOB, ldr.getType()); assertEquals(act.length, ldr.getSize()); assertFalse("not is large", ldr.isLarge()); assertSame(act, ldr.getCachedBytes()); assertSame(act, ldr.getCachedBytes(1)); assertSame(act, ldr.getCachedBytes(Integer.MAX_VALUE)); byte[] copy = ldr.getBytes(); assertNotSame(act, copy); assertTrue("same content", Arrays.equals(act, copy)); copy = ldr.getBytes(1); assertNotSame(act, copy); assertTrue("same content", Arrays.equals(act, copy)); copy = ldr.getBytes(Integer.MAX_VALUE); assertNotSame(act, copy); assertTrue("same content", Arrays.equals(act, copy)); ObjectStream in = ldr.openStream(); assertNotNull("has stream", in); assertTrue("is small stream", in instanceof ObjectStream.SmallStream); assertEquals(OBJ_BLOB, in.getType()); assertEquals(act.length, in.getSize()); assertEquals(act.length, in.available()); assertTrue("mark supported", in.markSupported()); copy = new byte[act.length]; assertEquals(act.length, in.read(copy)); assertEquals(0, in.available()); assertEquals(-1, in.read()); assertTrue("same content", Arrays.equals(act, copy)); ByteArrayOutputStream tmp = new ByteArrayOutputStream(); ldr.copyTo(tmp); assertTrue("same content", Arrays.equals(act, tmp.toByteArray())); }
public void testLargeObjectLoader() throws MissingObjectException, IOException { final byte[] act = rng.nextBytes(512); final ObjectLoader ldr = new ObjectLoader() { @Override public byte[] getCachedBytes() throws LargeObjectException { throw new LargeObjectException(); } @Override public long getSize() { return act.length; } @Override public int getType() { return OBJ_BLOB; } @Override public ObjectStream openStream() throws MissingObjectException, IOException { return new ObjectStream.Filter(getType(), act.length, new ByteArrayInputStream(act)); } }; assertEquals(OBJ_BLOB, ldr.getType()); assertEquals(act.length, ldr.getSize()); assertTrue("is large", ldr.isLarge()); try { ldr.getCachedBytes(); fail("did not throw on getCachedBytes()"); } catch (LargeObjectException tooBig) { // expected } try { ldr.getBytes(); fail("did not throw on getBytes()"); } catch (LargeObjectException tooBig) { // expected } try { ldr.getCachedBytes(64); fail("did not throw on getCachedBytes(64)"); } catch (LargeObjectException tooBig) { // expected } byte[] copy = ldr.getCachedBytes(1024); assertNotSame(act, copy); assertTrue("same content", Arrays.equals(act, copy)); ObjectStream in = ldr.openStream(); assertNotNull("has stream", in); assertEquals(OBJ_BLOB, in.getType()); assertEquals(act.length, in.getSize()); assertEquals(act.length, in.available()); assertTrue("mark supported", in.markSupported()); copy = new byte[act.length]; assertEquals(act.length, in.read(copy)); assertEquals(0, in.available()); assertEquals(-1, in.read()); assertTrue("same content", Arrays.equals(act, copy)); ByteArrayOutputStream tmp = new ByteArrayOutputStream(); ldr.copyTo(tmp); assertTrue("same content", Arrays.equals(act, tmp.toByteArray())); }
public void testLimitedGetCachedBytes() throws LargeObjectException, MissingObjectException, IOException { byte[] act = rng.nextBytes(512); ObjectLoader ldr = new ObjectLoader.SmallObject(OBJ_BLOB, act) { @Override public boolean isLarge() { return true; } }; assertTrue("is large", ldr.isLarge()); try { ldr.getCachedBytes(10); fail("Did not throw LargeObjectException"); } catch (LargeObjectException tooBig) { // Expected result. } byte[] copy = ldr.getCachedBytes(512); assertNotSame(act, copy); assertTrue("same content", Arrays.equals(act, copy)); copy = ldr.getCachedBytes(1024); assertNotSame(act, copy); assertTrue("same content", Arrays.equals(act, copy)); }
@Test public void testRandomWrites() throws IOException { final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final TestRng rng = new TestRng(getName()); final int max = TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2; final byte[] expect = new byte[max]; try { int written = 0; boolean onebyte = true; while (written < max) { if (onebyte) { final byte v = (byte) rng.nextInt(); b.write(v); expect[written++] = v; } else { final int len = Math.min(rng.nextInt() & 127, max - written); final byte[] tmp = rng.nextBytes(len); b.write(tmp, 0, len); System.arraycopy(tmp, 0, expect, written, len); written += len; } onebyte = !onebyte; } assertEquals(expect.length, written); b.close(); assertEquals(expect.length, b.length()); { final byte[] r = b.toByteArray(); assertNotNull(r); assertEquals(expect.length, r.length); assertArrayEquals(expect, r); } { final ByteArrayOutputStream o = new ByteArrayOutputStream(); b.writeTo(o, null); o.close(); final byte[] r = o.toByteArray(); assertEquals(expect.length, r.length); assertArrayEquals(expect, r); } } finally { b.destroy(); } }
private void init(int baseSize, int dataSize) throws IOException { base = rng.nextBytes(baseSize); data = new byte[dataSize]; deltaEnc = new DeltaEncoder(deltaBuf, baseSize, dataSize); }