Search in sources :

Example 1 with AbstractBlob

use of org.apache.jackrabbit.oak.plugins.memory.AbstractBlob in project jackrabbit-oak by apache.

the class JackrabbitNodeState method createBlob.

private Blob createBlob(final InternalValue value) {
    checkArgument(checkNotNull(value).getType() == PropertyType.BINARY);
    return new AbstractBlob() {

        @Override
        public long length() {
            try {
                return value.getLength();
            } catch (RepositoryException e) {
                warn("Unable to access blob length", e);
                return 0;
            }
        }

        @Nonnull
        @Override
        public InputStream getNewStream() {
            try {
                return value.getStream();
            } catch (RepositoryException e) {
                warn("Unable to access blob contents", e);
                return new ByteArrayInputStream(new byte[0]);
            }
        }

        @Override
        public String getReference() {
            if (!useBinaryReferences) {
                return null;
            }
            try {
                Binary binary = value.getBinary();
                try {
                    if (binary instanceof ReferenceBinary) {
                        return ((ReferenceBinary) binary).getReference();
                    } else {
                        return null;
                    }
                } finally {
                    binary.dispose();
                }
            } catch (RepositoryException e) {
                warn("Unable to get blob reference", e);
                return null;
            }
        }

        @Override
        public String getContentIdentity() {
            final String reference = getReference();
            if (reference != null) {
                final int pos = reference.indexOf(":");
                final String blobHash;
                if (pos > -1) {
                    blobHash = reference.substring(0, pos);
                } else {
                    blobHash = reference;
                }
                return blobHash + "#" + length();
            }
            return super.getContentIdentity();
        }
    };
}
Also used : AbstractBlob(org.apache.jackrabbit.oak.plugins.memory.AbstractBlob) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryException(javax.jcr.RepositoryException) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 Binary (javax.jcr.Binary)1 RepositoryException (javax.jcr.RepositoryException)1 ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)1 AbstractBlob (org.apache.jackrabbit.oak.plugins.memory.AbstractBlob)1