public void run() { try(InputStream inputStream = ByteStreams.limit(new RandomInputStream(), 100000)) {
public int read(byte[] b, int off, int len) { if (pos >= this.len) { return -1; } len = getReadBlock(len); if (len == 0) { return -1; } for (int i = 0; i < len; i++) { b[off + i] = (byte) (next() & 255); } pos += len; return len; }
public int read(byte[] b) { return read(b, 0, b.length); }
public long skip(long n) { n = getReadBlock(n); if (n == 0) { return -1; } pos += n; return n; }
public int read() { if (pos >= len) { return -1; } pos++; return next() & 255; }
private void setupNodes() { try { Node rootNode = sessionMover.getRootNode(); rootNode.addNode("node0"); rootNode.addNode("node1"); Node node2 = rootNode.addNode("node2"); Node node3 = node2.addNode("node3"); Node nodeWithBinary = node3.addNode("node-with-binary"); ValueFactory vf = sessionGarbageCollector.getValueFactory(); nodeWithBinary.setProperty("prop", vf.createBinary(new RandomInputStream(10, 1000))); sessionMover.save(); sleepForFile(); } catch (RepositoryException e) { failWithException(e); } }
public void test() throws Exception { Node root = superuser.getRootNode(); ValueFactory vf = superuser.getValueFactory(); // store a binary in the data store root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); // read from the binary, but don't close the file Value v1 = root.getProperty("p1").getValue(); InputStream in = v1.getBinary().getStream(); in.read(); // store the same content at a different place - // this will change the last modified date of the file root.setProperty("p2", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); in.close(); }
/** * Test reading from two concurrently opened streams. */ public void testTwoGetStreams() throws Exception { Node root = superuser.getRootNode(); ValueFactory vf = superuser.getValueFactory(); root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); root.setProperty("p2", vf.createBinary(new RandomInputStream(2, STREAM_LENGTH))); superuser.save(); InputStream i1 = root.getProperty("p1").getBinary().getStream(); InputStream i2 = root.getProperty("p2").getBinary().getStream(); assertEquals("p1", i1, new RandomInputStream(1, STREAM_LENGTH)); assertEquals("p2", i2, new RandomInputStream(2, STREAM_LENGTH)); try { i1.close(); } catch (IOException e) { log.info("Could not close first input stream: ", e); } try { i2.close(); } catch (IOException e) { log.info("Could not close second input stream: ", e); } }
/** * Tests reading concurrently from two different streams. */ public void testTwoStreamsConcurrently() throws Exception { Node root = superuser.getRootNode(); ValueFactory vf = superuser.getValueFactory(); root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); root.setProperty("p2", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); InputStream i1 = root.getProperty("p1").getBinary().getStream(); InputStream i2 = root.getProperty("p2").getBinary().getStream(); assertEquals("Streams are different", i1, i2); try { i1.close(); } catch (IOException e) { log.info("Could not close first input stream: ", e); } try { i2.close(); } catch (IOException e) { log.info("Could not close second input stream: ", e); } }
void doTest(DataStore ds, int offset) throws Exception { ArrayList<DataRecord> list = new ArrayList<DataRecord>(); HashMap<DataRecord, Integer> map = new HashMap<DataRecord, Integer>(); for (int i = 0; i < 100; i++) { int size = 100 + i * 10; RandomInputStream in = new RandomInputStream(size + offset, size); DataRecord rec = ds.addRecord(in); list.add(rec); map.put(rec, new Integer(size)); } Random random = new Random(1); for (int i = 0; i < list.size(); i++) { int pos = random.nextInt(list.size()); DataRecord rec = list.get(pos); int size = map.get(rec); rec = ds.getRecord(rec.getIdentifier()); assertEquals(size, rec.getLength()); InputStream in = rec.getStream(); RandomInputStream expected = new RandomInputStream(size + offset, size); if (random.nextBoolean()) { in = readInputStreamRandomly(in, random); } assertEquals(expected, in); in.close(); } }
for (int i = 0; i < 10; i++) { int size = 100000 - (i * 100); RandomInputStream in = new RandomInputStream(size + offset, size); DataRecord rec = ds.addRecord(in); list.add(rec); rec = ds.getRecord(rec.getIdentifier()); Assert.assertEquals(size, rec.getLength()); RandomInputStream expected = new RandomInputStream(size + offset, size); InputStream in = rec.getStream();
for (int i = 0; i < 10; i++) { int size = 100000 - (i * 100); RandomInputStream in = new RandomInputStream(size + offset, size); DataRecord rec = ds.addRecord(in); list.add(rec); rec = ds.getRecord(rec.getIdentifier()); assertEquals(size, rec.getLength()); RandomInputStream expected = new RandomInputStream(size + offset, size); InputStream in = rec.getStream();
/** * Tests reading concurrently from two different streams coming from the * same property. */ public void testTwoStreamsFromSamePropertyConcurrently() throws Exception { Node root = superuser.getRootNode(); ValueFactory vf = superuser.getValueFactory(); root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); InputStream i1 = root.getProperty("p1").getBinary().getStream(); InputStream i2 = root.getProperty("p1").getBinary().getStream(); assertEquals("Streams are different", i1, i2); try { i1.close(); } catch (IOException e) { log.info("Could not close first input stream: ", e); } try { i2.close(); } catch (IOException e) { log.info("Could not close second input stream: ", e); } }
root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); long saveOne = System.currentTimeMillis() - time; root.setProperty("p2", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save();
public void testTransientObjects() throws Exception { Node root = testRootNode; Session session = root.getSession(); deleteMyNodes(); Credentials cred = getHelper().getSuperuserCredentials(); Session s2 = getHelper().getRepository().login(cred); root = s2.getRootNode(); Node node2 = root.addNode("node3"); Node n = node2.addNode("nodeWithBlob"); ValueFactory vf = session.getValueFactory(); n.setProperty("test", vf.createBinary(new RandomInputStream(10, 1000))); runGC(session, false); s2.save(); InputStream in = n.getProperty("test").getBinary().getStream(); InputStream in2 = new RandomInputStream(10, 1000); verifyInputStream(in, in2); deleteMyNodes(); s2.logout(); }
public void testReferenceBinaryExchangeWithSharedRepository() throws Exception { Session firstSession = superuser; // create a binary Binary b = vf.createBinary(new RandomInputStream(1, STREAM_LENGTH)); ReferenceBinary referenceBinary = null; if (b instanceof ReferenceBinary) { referenceBinary = (ReferenceBinary) b; } assertNotNull(referenceBinary); assertNotNull(referenceBinary.getReference()); // in the current test the message is exchanged via repository which is shared as well // put the reference message value in a property on a node String newNode = "sample_" + System.nanoTime(); firstSession.getRootNode().addNode(newNode).setProperty("reference", referenceBinary.getReference()); // save the first session firstSession.save(); // get a second session over the same repository / ds Session secondSession = getHelper().getRepository().login(new SimpleCredentials("admin", "admin".toCharArray())); // read the binary referenced by the referencing binary String reference = secondSession.getRootNode().getNode(newNode).getProperty("reference").getString(); ReferenceBinary ref = new SimpleReferenceBinary(reference); assertEquals(b, secondSession.getValueFactory().createValue(ref).getBinary()); }
Node n = node2.addNode("nodeWithBlob").addNode("sub"); ValueFactory vf = session.getValueFactory(); Binary b = vf.createBinary(new RandomInputStream(20, 1000)); n.setProperty("test", b); session.save(); n = node2.addNode("nodeWithTemporaryBlob"); n.setProperty("test", vf.createBinary(new RandomInputStream(11, 1000))); session.save(); b = n.getProperty("test").getValue().getBinary(); InputStream in = b.getStream(); InputStream in2 = new RandomInputStream(20, 1000); verifyInputStream(in, in2);
/** * Taken from org.apache.jackrabbit.core.value.ReferenceBinaryTest * @throws Exception */ @Test public void testReferenceBinaryExchangeWithSharedRepository() throws Exception { Session firstSession = createAdminSession(); // create a binary Binary b = firstSession.getValueFactory().createBinary(new RandomInputStream(1, STREAM_LENGTH)); ReferenceBinary referenceBinary = null; if (b instanceof ReferenceBinary) { referenceBinary = (ReferenceBinary) b; } assertNotNull(referenceBinary); assertNotNull(referenceBinary.getReference()); // in the current test the message is exchanged via repository which is shared as well // put the reference message value in a property on a node String newNode = "sample_" + System.nanoTime(); firstSession.getRootNode().addNode(newNode).setProperty("reference", referenceBinary.getReference()); // save the first session firstSession.save(); // get a second session over the same repository / ds Session secondSession = repository.login(new SimpleCredentials("admin", "admin".toCharArray())); // read the binary referenced by the referencing binary String reference = secondSession.getRootNode().getNode(newNode).getProperty("reference").getString(); ReferenceBinary ref = new SimpleReferenceBinary(reference); assertEquals(b, secondSession.getValueFactory().createValue(ref).getBinary()); safeLogout(firstSession); safeLogout(secondSession); }
/** * @see <a * href="https://issues.apache.org/jira/browse/JCR-3296">JCR-3296</a> * Indexing ignored file types creates some garbage */ public void testEmptyParser() throws Exception { InternalValue val = InternalValue .create(new RandomInputStream(1, 1024)); Metadata metadata = new Metadata(); metadata.set(Metadata.CONTENT_TYPE, "application/java-archive"); metadata.set(Metadata.CONTENT_ENCODING, "UTF-8"); Parser p = getSearchIndex().getParser(); ParsingTask task = new ParsingTask(p, val, metadata, Integer.MAX_VALUE) { public void setExtractedText(String value) { assertEquals("", value); } }; task.run(); } }