use of javax.jcr.Binary in project jackrabbit by apache.
the class BinaryPropertyTest method testGetLengthJcr2.
/**
* Tests the Binary.getSize() method.
*/
public void testGetLengthJcr2() throws RepositoryException {
Value val = PropertyUtil.getValue(prop);
Binary binary = val.getBinary();
long length;
try {
length = binary.getSize();
} finally {
binary.dispose();
}
long bytes = PropertyUtil.countBytes(val);
if (bytes != -1) {
assertEquals("Binary.getSize() returns wrong number of bytes.", bytes, length);
}
}
use of javax.jcr.Binary in project jackrabbit by apache.
the class BinaryPropertyTest method testValueJcr2.
/**
* Tests that Value.getStream() delivers the same as Value.getBinary.getStream().
* We check this by reading each byte of the two streams and assuring that
* they are equal.
*/
public void testValueJcr2() throws IOException, RepositoryException {
Value val = PropertyUtil.getValue(prop);
InputStream in = val.getStream();
Binary bin = val.getBinary();
try {
InputStream in2 = bin.getStream();
try {
int b = in.read();
while (b != -1) {
int b2 = in2.read();
assertEquals("Value.getStream() and Value.getBinary().getStream() " + "return different values.", b, b2);
b = in.read();
}
assertEquals("Value.getStream() and Value.getBinary().getStream() " + "return different values.", -1, in2.read());
} finally {
try {
in.close();
} catch (IOException ignore) {
}
try {
in2.close();
} catch (IOException ignore) {
}
}
} finally {
bin.dispose();
}
}
use of javax.jcr.Binary in project jackrabbit by apache.
the class ValueFactoryTest method testInputStream.
/**
* Tests whether a passed <code>InputStream</code> is closed
* by the implementation.
*
* @throws RepositoryException
*/
public void testInputStream() throws RepositoryException {
InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
valueFactory.createValue(in);
assertTrue("ValueFactory.createValue(InputStream) is expected to close the passed input stream", in.isClosed());
in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
Binary bin = valueFactory.createBinary(in);
assertTrue("ValueFactory.createBinary(InputStream) is expected to close the passed input stream", in.isClosed());
bin.dispose();
}
use of javax.jcr.Binary in project jackrabbit by apache.
the class BinaryTest method testRevertSettingExistingBinary.
public void testRevertSettingExistingBinary() throws Exception {
Node test = testRootNode.addNode("test");
Binary b = superuser.getValueFactory().createBinary(generateValue());
Property p = test.setProperty("prop", b);
QValue qv1 = getQValue(p);
superuser.save();
Binary b2 = superuser.getValueFactory().createBinary(generateValue());
test.setProperty("prop", b2);
QValue qv2 = getQValue(p);
assertFalse(qv1.equals(qv2));
superuser.refresh(false);
assertEquals(qv1, getQValue(p));
assertSame(qv1, getQValue(p));
assertFalse(qv2.equals(getQValue(p)));
}
use of javax.jcr.Binary in project jackrabbit by apache.
the class AbstractQValue method getBinary.
/**
* This implementation creates a binary instance that uses
* {@link #getStream()} and skipping on the given stream as its underlying
* mechanism to provide random access defined on {@link Binary}.
*
* @see QValue#getBinary()
*/
public Binary getBinary() throws RepositoryException {
return new Binary() {
public InputStream getStream() throws RepositoryException {
return AbstractQValue.this.getStream();
}
public int read(byte[] b, long position) throws IOException, RepositoryException {
InputStream in = getStream();
try {
long skip = position;
while (skip > 0) {
long skipped = in.skip(skip);
if (skipped <= 0) {
return -1;
}
skip -= skipped;
}
return in.read(b);
} finally {
in.close();
}
}
public long getSize() throws RepositoryException {
return getLength();
}
public void dispose() {
}
};
}
Aggregations