private static DiskFileItem makePayload(int thresh, String repoPath, String filePath, byte[] data) throws IOException, Exception { // if thresh < written length, delete outputFile after copying to repository temp file // otherwise write the contents to repository temp file File repository = new File(repoPath); DiskFileItem diskFileItem = new DiskFileItem("test", "application/octet-stream", false, "test", 100000, repository, null); File outputFile = new File(filePath); DeferredFileOutputStream dfos = new DeferredFileOutputStream(thresh, outputFile); OutputStream os = (OutputStream) Reflections.getFieldValue(dfos, "memoryOutputStream"); os.write(data); Reflections.getField(ThresholdingOutputStream.class, "written").set(dfos, data.length); Reflections.setFieldValue(diskFileItem, "dfos", dfos); Reflections.setFieldValue(diskFileItem, "sizeThreshold", 0); return diskFileItem; }
/** * Returns the size of the file. * * @return The size of the file, in bytes. */ public long getSize() { if (cachedContent != null) { return cachedContent.length; } else if (dfos.isInMemory()) { return dfos.getData().length; } else { return dfos.getFile().length(); } }
/** * Returns an {@link java.io.InputStream InputStream} that can be used to retrieve the contents * of the file. * * @return An {@link java.io.InputStream InputStream} that can be used to retrieve the contents * of the file. * * @throws IOException * if an error occurs. */ public InputStream getInputStream() throws IOException { if (!isInMemory()) { return new FileInputStream(dfos.getFile()); } if (cachedContent == null) { cachedContent = dfos.getData(); } return new ByteArrayInputStream(cachedContent); }
/** * Writes the state of this object during serialization. * * @param out * The stream to which the state should be written. * * @throws IOException * if an error occurs. */ private void writeObject(ObjectOutputStream out) throws IOException { // Read the data if (dfos.isInMemory()) { cachedContent = get(); } else { cachedContent = null; dfosFile = dfos.getFile(); } // write out values out.defaultWriteObject(); }
/** * Determines whether or not the data for this output stream has been retained in memory. * * @return <code>true</code> if the data is available in memory; <code>false</code> otherwise. */ public boolean isInMemory() { return (!isThresholdExceeded()); }
/** * Returns the {@link java.io.File} object for the <code>FileItem</code>'s data's temporary * location on the disk. Note that for <code>FileItem</code>s that have their data stored in * memory, this method will return <code>null</code>. When handling large files, you can use * {@link java.io.File#renameTo(java.io.File)} to move the file to new location without copying * the data, if the source and destination locations reside within the same logical volume. * * @return The data file, or <code>null</code> if the data is stored in memory. */ public File getStoreLocation() { return dfos == null ? null : dfos.getFile(); }
/** * Provides a hint as to whether or not the file contents will be read from memory. * * @return <code>true</code> if the file contents will be read from memory; <code>false</code> * otherwise. */ public boolean isInMemory() { return (dfos.isInMemory()); }
/** * Returns the size of the file. * * @return The size of the file, in bytes. */ public long getSize() { if (size >= 0) { return size; } else if (cachedContent != null) { return cachedContent.length; } else if (dfos.isInMemory()) { return dfos.getData().length; } else { return dfos.getFile().length(); } }
/** * Determines whether or not the data for this output stream has been retained in memory. * * @return <code>true</code> if the data is available in memory; <code>false</code> * otherwise. */ public boolean isInMemory() { return (!isThresholdExceeded()); }
/** * Returns the {@link java.io.File} object for the <code>FileItem</code>'s data's temporary * location on the disk. Note that for <code>FileItem</code>s that have their data stored in * memory, this method will return <code>null</code>. When handling large files, you can use * {@link java.io.File#renameTo(java.io.File)} to move the file to new location without copying * the data, if the source and destination locations reside within the same logical volume. * * @return The data file, or <code>null</code> if the data is stored in memory. */ public File getStoreLocation() { return dfos.getFile(); }
/** * Provides a hint as to whether or not the file contents will be read from memory. * * @return <code>true</code> if the file contents will be read from memory; <code>false</code> * otherwise. */ public boolean isInMemory() { if (cachedContent != null) { return true; } return dfos.isInMemory(); }
/** * Returns an {@link java.io.InputStream InputStream} that can be used to retrieve the contents * of the file. * * @return An {@link java.io.InputStream InputStream} that can be used to retrieve the contents * of the file. * * @exception IOException * if an error occurs. */ public InputStream getInputStream() throws IOException { if (!dfos.isInMemory()) { return new FileInputStream(dfos.getFile()); } if (cachedContent == null) { cachedContent = dfos.getData(); } return new ByteArrayInputStream(cachedContent); }
/** * Returns an {@link java.io.OutputStream OutputStream} that can be used for storing the * contents of the file. * * @return An {@link java.io.OutputStream OutputStream} that can be used for storing the * contensts of the file. * * @throws IOException * if an error occurs. */ public OutputStream getOutputStream() throws IOException { if (dfos == null) { dfos = new DeferredFileOutputStream(sizeThreshold, new DeferredFileOutputStream.FileFactory() { public File createFile() { return getTempFile(); } }); } return dfos; }
/** * Determines whether or not the data for this output stream has been retained in memory. * * @return <code>true</code> if the data is available in memory; <code>false</code> otherwise. */ public boolean isInMemory() { return (!isThresholdExceeded()); }
/** * Removes the file contents from the temporary storage. */ protected void finalize() { File outputFile = dfos.getFile(); if (outputFile != null && outputFile.exists()) { outputFile.delete(); } }
if (dfos.isInMemory()) cachedContent = dfos.getData(); fis = new FileInputStream(dfos.getFile()); fis.read(fileData);
/** * Returns an {@link java.io.OutputStream OutputStream} that can be used for storing the * contents of the file. * * @return An {@link java.io.OutputStream OutputStream} that can be used for storing the * contensts of the file. * * @exception IOException * if an error occurs. */ public OutputStream getOutputStream() throws IOException { if (dfos == null) { File outputFile = getTempFile(); dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); } return dfos; }
/** * Removes the file contents from the temporary storage. */ @Override protected void finalize() { File outputFile = dfos.getFile(); if (outputFile != null && outputFile.exists()) { outputFile.delete(); } }