Search in sources :

Example 6 with TransientFileFactory

use of org.apache.jackrabbit.util.TransientFileFactory in project jackrabbit-oak by apache.

the class AbstractSharedCachingDataStore method addRecord.

@Override
public DataRecord addRecord(InputStream inputStream, BlobOptions blobOptions) throws DataStoreException {
    Stopwatch watch = Stopwatch.createStarted();
    try {
        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
        File tmpFile = fileFactory.createTransientFile("upload", null, tmp);
        // Copy the stream to the temporary file and calculate the
        // stream length and the message digest of the stream
        MessageDigest digest = MessageDigest.getInstance(DIGEST);
        OutputStream output = new DigestOutputStream(new FileOutputStream(tmpFile), digest);
        long length = 0;
        try {
            length = IOUtils.copyLarge(inputStream, output);
        } finally {
            output.close();
        }
        DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
        LOG.debug("SHA-256 of [{}], length =[{}] took [{}] ms ", identifier, length, watch.elapsed(TimeUnit.MILLISECONDS));
        // otherwise add to backend
        if (blobOptions.getUpload() == SYNCHRONOUS || !cache.stage(identifier.toString(), tmpFile)) {
            backend.write(identifier, tmpFile);
            LOG.info("Added blob [{}] to backend", identifier);
            // offer to download cache
            cache.getDownloadCache().put(identifier.toString(), tmpFile);
        }
        return getRecordIfStored(identifier);
    } catch (Exception e) {
        LOG.error("Error in adding record");
        throw new DataStoreException("Error in adding record ", e);
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) Stopwatch(com.google.common.base.Stopwatch) MessageDigest(java.security.MessageDigest) File(java.io.File) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException)

Example 7 with TransientFileFactory

use of org.apache.jackrabbit.util.TransientFileFactory in project jackrabbit by apache.

the class ValueHelper method deserialize.

/**
     * Deserializes the string data read from the given reader to a
     * <code>Value</code> of the given type.
     *
     * @param reader       reader for the string data to be deserialized
     * @param type         type of value
     * @param decodeBlanks if <code>true</code> <code>"_x0020_"</code>
     *                     character sequences will be decoded to single space
     *                     characters each.
     * @param factory      ValueFactory used to build the <code>Value</code> object.
     * @return the deserialized <code>Value</code>
     * @throws IOException          if an i/o error occured during the
     *                              serialization
     * @throws ValueFormatException if the string data is not of the required
     *                              format
     * @throws RepositoryException  if an error occured during the
     *                              deserialization.
     */
public static Value deserialize(Reader reader, int type, boolean decodeBlanks, ValueFactory factory) throws IOException, ValueFormatException, RepositoryException {
    if (type == PropertyType.BINARY) {
        // base64 encoded binary value;
        // the encodeBlanks flag can be ignored since base64-encoded
        // data cannot contain encoded space characters
        // decode to temp file
        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
        final File tmpFile = fileFactory.createTransientFile("bin", null, null);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile));
        try {
            Base64.decode(reader, out);
        } finally {
            out.close();
        }
        // pass InputStream wrapper to ValueFactory, that creates a BinaryValue.
        return factory.createValue(new FilterInputStream(new FileInputStream(tmpFile)) {

            public void close() throws IOException {
                in.close();
                // temp file can now safely be removed
                tmpFile.delete();
            }
        });
    /*
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Base64.decode(reader, baos);
            // no need to close ByteArrayOutputStream
            //baos.close();
            return new BinaryValue(baos.toByteArray());
*/
    } else {
        char[] chunk = new char[8192];
        int read;
        StringBuilder buf = new StringBuilder();
        while ((read = reader.read(chunk)) > -1) {
            buf.append(chunk, 0, read);
        }
        String value = buf.toString();
        if (decodeBlanks) {
            // decode encoded blanks in value
            value = Text.replace(value, "_x0020_", " ");
        }
        return convert(value, type, factory);
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Aggregations

FileOutputStream (java.io.FileOutputStream)7 TransientFileFactory (org.apache.jackrabbit.util.TransientFileFactory)7 File (java.io.File)5 IOException (java.io.IOException)5 BufferedOutputStream (java.io.BufferedOutputStream)4 OutputStream (java.io.OutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 FilterInputStream (java.io.FilterInputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Stopwatch (com.google.common.base.Stopwatch)1 FilterOutputStream (java.io.FilterOutputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 DigestOutputStream (java.security.DigestOutputStream)1 MessageDigest (java.security.MessageDigest)1 SQLException (java.sql.SQLException)1 RepositoryException (javax.jcr.RepositoryException)1 Value (javax.jcr.Value)1 DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)1