Search in sources :

Example 1 with TransientFileFactory

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

the class BufferedStringValue method append.

/**
     * Append a portion of an array of characters.
     *
     * @param chars  the characters to be appended
     * @param start  the index of the first character to append
     * @param len the number of characters to append
     * @throws IOException if an I/O error occurs
     */
public void append(char[] chars, int start, int len) throws IOException {
    if (buffer != null) {
        if (this.length + len > MAX_BUFFER_SIZE) {
            // threshold for keeping data in memory exceeded;
            // create temp file and spool buffer contents
            TransientFileFactory fileFactory = TransientFileFactory.getInstance();
            tmpFile = fileFactory.createTransientFile("txt", null, null);
            BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(tmpFile));
            writer = new OutputStreamWriter(fout, "UTF-8");
            writer.write(buffer.toString());
            writer.write(chars, start, len);
            // reset the in-memory buffer
            buffer = null;
        } else {
            buffer.write(chars, start, len);
        }
    } else if (tmpFile != null) {
        writer.write(chars, start, len);
    } else {
        throw new IOException("this instance has already been disposed");
    }
    length += len;
}
Also used : TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with TransientFileFactory

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

the class DatabaseFileSystem method getOutputStream.

/**
     * {@inheritDoc}
     */
public OutputStream getOutputStream(final String filePath) throws FileSystemException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }
    FileSystemPathUtil.checkFormat(filePath);
    final String parentDir = FileSystemPathUtil.getParentDir(filePath);
    final String name = FileSystemPathUtil.getName(filePath);
    if (!isFolder(parentDir)) {
        throw new FileSystemException("path not found: " + parentDir);
    }
    if (isFolder(filePath)) {
        throw new FileSystemException("path denotes folder: " + filePath);
    }
    try {
        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
        final File tmpFile = fileFactory.createTransientFile("bin", null, null);
        return new FilterOutputStream(new FileOutputStream(tmpFile)) {

            public void write(byte[] bytes, int off, int len) throws IOException {
                out.write(bytes, off, len);
            }

            public void close() throws IOException {
                out.flush();
                ((FileOutputStream) out).getFD().sync();
                out.close();
                InputStream in = null;
                try {
                    if (isFile(filePath)) {
                        synchronized (updateDataSQL) {
                            long length = tmpFile.length();
                            in = new FileInputStream(tmpFile);
                            conHelper.exec(updateDataSQL, new Object[] { new StreamWrapper(in, length), new Long(System.currentTimeMillis()), new Long(length), parentDir, name });
                        }
                    } else {
                        synchronized (insertFileSQL) {
                            long length = tmpFile.length();
                            in = new FileInputStream(tmpFile);
                            conHelper.exec(insertFileSQL, new Object[] { parentDir, name, new StreamWrapper(in, length), new Long(System.currentTimeMillis()), new Long(length) });
                        }
                    }
                } catch (Exception e) {
                    IOException ioe = new IOException(e.getMessage());
                    ioe.initCause(e);
                    throw ioe;
                } finally {
                    if (in != null) {
                        in.close();
                    }
                    // temp file can now safely be removed
                    tmpFile.delete();
                }
            }
        };
    } catch (Exception e) {
        String msg = "failed to open output stream to file: " + filePath;
        log.error(msg, e);
        throw new FileSystemException(msg, e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) SQLException(java.sql.SQLException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) FileSystemException(org.apache.jackrabbit.core.fs.FileSystemException) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) FileOutputStream(java.io.FileOutputStream) StreamWrapper(org.apache.jackrabbit.core.util.db.StreamWrapper) FilterOutputStream(java.io.FilterOutputStream) File(java.io.File)

Example 3 with TransientFileFactory

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

the class SessionImporter method buildQValue.

/**
     *
     * @param tv
     * @param targetType
     * @param resolver The name/path resolver used to build a <code>QValue</code>.
     * @return
     * @throws RepositoryException
     */
private QValue buildQValue(TextValue tv, int targetType, NamePathResolver resolver) throws RepositoryException {
    QValue iv;
    try {
        switch(targetType) {
            case PropertyType.BINARY:
                // base64 encoded BINARY type
                if (tv.length() < 0x10000) {
                    // < 65kb: deserialize BINARY type in memory
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    Base64.decode(tv.retrieve(), baos);
                    // no need to close ByteArrayOutputStream
                    //baos.close();
                    iv = session.getQValueFactory().create(baos.toByteArray());
                } else {
                    // >= 65kb: deserialize BINARY type
                    // using Reader and temporary file
                    TransientFileFactory fileFactory = TransientFileFactory.getInstance();
                    File tmpFile = fileFactory.createTransientFile("bin", null, null);
                    FileOutputStream out = new FileOutputStream(tmpFile);
                    Reader reader = tv.reader();
                    try {
                        Base64.decode(reader, out);
                    } finally {
                        reader.close();
                        out.close();
                    }
                    iv = session.getQValueFactory().create(tmpFile);
                }
                break;
            default:
                // build iv using namespace context of xml document
                Value v = ValueHelper.convert(tv.retrieve(), targetType, session.getValueFactory());
                iv = ValueFormat.getQValue(v, resolver, session.getQValueFactory());
                break;
        }
        return iv;
    } catch (IOException e) {
        String msg = "failed to retrieve serialized value";
        log.debug(msg, e);
        throw new RepositoryException(msg, e);
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) FileOutputStream(java.io.FileOutputStream) SetPropertyValue(org.apache.jackrabbit.jcr2spi.operation.SetPropertyValue) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) Reader(java.io.Reader) RepositoryException(javax.jcr.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 4 with TransientFileFactory

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

the class BufferedStringValue method append.

/**
     * Append a portion of an array of characters.
     *
     * @param chars the characters to be appended
     * @param start the index of the first character to append
     * @param len   the number of characters to append
     * @throws IOException if an I/O error occurs
     */
public void append(char[] chars, int start, int len) throws IOException {
    if (buffer != null) {
        if (this.length + len > MAX_BUFFER_SIZE) {
            // threshold for keeping data in memory exceeded;
            // create temp file and spool buffer contents
            TransientFileFactory fileFactory = TransientFileFactory.getInstance();
            tmpFile = fileFactory.createTransientFile("txt", null, null);
            BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(tmpFile));
            writer = new OutputStreamWriter(fout, "UTF-8");
            writer.write(buffer.toString());
            writer.write(chars, start, len);
            // reset the in-memory buffer
            buffer = null;
        } else {
            buffer.write(chars, start, len);
        }
    } else if (tmpFile != null) {
        writer.write(chars, start, len);
    } else {
        throw new IOException("this instance has already been disposed");
    }
    length += len;
}
Also used : TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with TransientFileFactory

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

the class LocalCache method store.

/**
     * Store an item in the cache and return the input stream. If cache is in
     * purgeMode or file doesn't exists, inputstream from a
     * {@link TransientFileFactory#createTransientFile(String, String, File)} is
     * returned. Otherwise inputStream from cached file is returned. This method
     * doesn't close the incoming inputstream.
     * 
     * @param fileName the key of cache.
     * @param in {@link InputStream}
     * @return the (new) input stream.
     */
public InputStream store(String fileName, final InputStream in) throws IOException {
    fileName = fileName.replace("\\", "/");
    File f = getFile(fileName);
    long length = 0;
    if (!f.exists() || isInPurgeMode()) {
        OutputStream out = null;
        File transFile = null;
        try {
            TransientFileFactory tff = TransientFileFactory.getInstance();
            transFile = tff.createTransientFile("s3-", "tmp", tmp);
            out = new BufferedOutputStream(new FileOutputStream(transFile));
            length = IOUtils.copyLarge(in, out);
        } finally {
            IOUtils.closeQuietly(out);
        }
        // rename the file to local fs cache
        if (canAdmitFile(length) && (f.getParentFile().exists() || f.getParentFile().mkdirs()) && transFile.renameTo(f) && f.exists()) {
            if (transFile.exists() && transFile.delete()) {
                LOG.info("tmp file [{}] not deleted successfully", transFile.getAbsolutePath());
            }
            transFile = null;
            LOG.debug("file [{}] doesn't exists. adding to local cache using inputstream.", fileName);
            cache.put(fileName, f.length());
        } else {
            LOG.debug("file [{}] doesn't exists. returning transient file [{}].", fileName, transFile.getAbsolutePath());
            f = transFile;
        }
    } else {
        f.setLastModified(System.currentTimeMillis());
        LOG.debug("file [{}]  exists. adding to local cache using inputstream.", fileName);
        cache.put(fileName, f.length());
    }
    tryPurge();
    return new LazyFileInputStream(f);
}
Also used : TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

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