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() {
}
};
}
use of javax.jcr.Binary in project jackrabbit by apache.
the class QValueValue method getBinary.
/**
* @see javax.jcr.Value#getBinary()
*/
public Binary getBinary() throws RepositoryException {
// JCR-2511 Value#getBinary() and #getStream() return internal representation for type PATH and NAME
if (getType() == PropertyType.NAME || getType() == PropertyType.PATH) {
// delegate conversion to getString() method
try {
final byte[] value = getString().getBytes("UTF-8");
return new Binary() {
public int read(byte[] b, long position) {
if (position >= value.length) {
return -1;
} else {
int p = (int) position;
int n = Math.min(b.length, value.length - p);
System.arraycopy(value, p, b, 0, n);
return n;
}
}
public InputStream getStream() {
return new ByteArrayInputStream(value);
}
public long getSize() {
return value.length;
}
public void dispose() {
}
};
} catch (UnsupportedEncodingException ex) {
throw new RepositoryException("UTF-8 is not supported", ex);
}
} else {
return qvalue.getBinary();
}
}
use of javax.jcr.Binary in project jackrabbit-oak by apache.
the class DefaultSyncContext method createValue.
/**
* Creates a new JCR value of the given object, checking the internal type.
* @param v the value
* @return the JCR value or null
* @throws RepositoryException if an error occurs
*/
@CheckForNull
protected Value createValue(@Nullable Object v) throws RepositoryException {
if (v == null) {
return null;
} else if (v instanceof Boolean) {
return valueFactory.createValue((Boolean) v);
} else if (v instanceof Byte || v instanceof Short || v instanceof Integer || v instanceof Long) {
return valueFactory.createValue(((Number) v).longValue());
} else if (v instanceof Float || v instanceof Double) {
return valueFactory.createValue(((Number) v).doubleValue());
} else if (v instanceof BigDecimal) {
return valueFactory.createValue((BigDecimal) v);
} else if (v instanceof Calendar) {
return valueFactory.createValue((Calendar) v);
} else if (v instanceof Date) {
Calendar cal = Calendar.getInstance();
cal.setTime((Date) v);
return valueFactory.createValue(cal);
} else if (v instanceof byte[]) {
Binary bin = valueFactory.createBinary(new ByteArrayInputStream((byte[]) v));
return valueFactory.createValue(bin);
} else if (v instanceof Binary) {
return valueFactory.createValue((Binary) v);
} else if (v instanceof InputStream) {
return valueFactory.createValue((InputStream) v);
} else if (v instanceof char[]) {
return valueFactory.createValue(new String((char[]) v));
} else {
return valueFactory.createValue(String.valueOf(v));
}
}
use of javax.jcr.Binary in project jackrabbit-oak by apache.
the class BundlingNodeTest method addFile.
private void addFile(Node parent, String fileName) throws RepositoryException {
Node file = getOrAddNode(parent, fileName, NodeType.NT_FILE);
Node content = getOrAddNode(file, Node.JCR_CONTENT, contentNodeType);
content.setProperty(Property.JCR_MIMETYPE, "text/plain");
content.setProperty(Property.JCR_LAST_MODIFIED, Calendar.getInstance());
Binary binary = parent.getSession().getValueFactory().createBinary(new ByteArrayInputStream("hello".getBytes()));
content.setProperty(Property.JCR_DATA, binary);
binary.dispose();
}
Aggregations