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;
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations