MultiFileOutputStream mfos = req.getMultiFileOutputStream(); if (mfos == null) mfos = new MultiFileOutputStream(); try { mfos.init(observer, req.getPartSize(), req.getDiskLimit()); if (defaultExecutorService) mfos.cleanup(); // delete left-over temp files
public void cleanup() { for (int i=0; i < getNumFilesWritten(); i++) { File f = getFile(i); if (f.exists()) { if (!f.delete()) { LogFactory.getLog(getClass()).debug( "Ignoring failure to delete file " + f); } } } }
/** * Returns the file output stream to be used for writing, blocking if * necessary if running out of disk space. * * @throws InterruptedException if the running thread was interrupted */ private FileOutputStream fos() throws IOException { if (closed) throw new IOException("Output stream is already closed"); if (os == null || currFileBytesWritten >= partSize) { if (os != null) { os.close(); // notify about the new file ready for processing observer.onPartCreate(new PartCreationEvent( getFile(filesCreated), filesCreated, false, this)); } currFileBytesWritten = 0; filesCreated++; blockIfNecessary(); final File file = getFile(filesCreated); os = new FileOutputStream(file); } return os; }
@Test public void testRead3() throws IOException { mfos.init(Mockito.mock(UploadObjectObserver.class), 100, Long.MAX_VALUE); mfos.cleanup(); int count = 0; while (count < 200) { final byte[] b = new byte[50]; new Random().nextBytes(b); mfos.write(b, 0, 50); count++; } Assert.assertNotNull(mfos.getFile(2)); Assert.assertEquals(mfos.getNumFilesWritten(), 100); Assert.assertEquals(mfos.getPartSize(), 100); Assert.assertNotNull(mfos.getNamePrefix()); Assert.assertEquals(mfos.getDiskLimit(), Long.MAX_VALUE); Assert.assertEquals(mfos.getTotalBytesWritten(), 10000); Assert.assertNotNull(mfos.getRoot()); }
@Test public void testRead2() throws IOException { mfos.init(Mockito.mock(UploadObjectObserver.class), 100, Long.MAX_VALUE); mfos.cleanup(); mfos.flush(); int count = 0; while (count < 200) { final byte[] b = new byte[50]; new Random().nextBytes(b); mfos.write(b); count++; } Assert.assertNotNull(mfos.getFile(2)); Assert.assertEquals(mfos.getNumFilesWritten(), 100); }
@Test public void testRead() throws IOException { mfos.init(Mockito.mock(UploadObjectObserver.class), 100, Long.MAX_VALUE); int count = 0; while (count < 10000) { mfos.write(count); count++; } Assert.assertNotNull(mfos.getFile(2)); Assert.assertEquals(mfos.getNumFilesWritten(), 100); }
/** * {@inheritDoc} * * This method would block as necessary if running out of disk space. */ @Override public void write(int b) throws IOException { fos().write(b); currFileBytesWritten++; totalBytesWritten++; }
@Override public void close() throws IOException { if (closed) return; closed = true; if (os != null) { os.close(); File lastPart = getFile(filesCreated); if (lastPart.length() == 0) { if (!lastPart.delete()) { LogFactory.getLog(getClass()).debug( "Ignoring failure to delete empty file " + lastPart); } } else { // notify about the new file ready for processing observer.onPartCreate(new PartCreationEvent( getFile(filesCreated), filesCreated, true, this)); } } }
/** * Construct an instance to use the default temporary directory and temp * file naming convention. The * {@link #init(UploadObjectObserver, long, long)} must be called before * this stream is considered fully initialized. */ public MultiFileOutputStream() { root = new File(System.getProperty("java.io.tmpdir")); namePrefix = yyMMdd_hhmmss() + "." + UUID.randomUUID(); }
@Before public void setup() { mfos = new MultiFileOutputStream(); }
/** * {@inheritDoc} * * This method would block as necessary if running out of disk space. */ @Override public void write(byte[] b, int off, int len) throws IOException { if (b.length == 0) return; fos().write(b, off, len); currFileBytesWritten += len; totalBytesWritten += len; }
@Override public void close() throws IOException { if (closed) { return; } closed = true; if (os != null) { os.close(); final File lastPart = getFile(filesCreated); if (lastPart.length() == 0) { if (!lastPart.delete()) { LogFactory.getLog(getClass()).debug( "Ignoring failure to delete empty file " + lastPart); } } else { // notify about the new file ready for processing observer.onPartCreate(new PartCreationEvent( getFile(filesCreated), filesCreated, true, this)); } } }
/** * Construct an instance to use the default temporary directory and temp * file naming convention. The * {@link #init(UploadObjectObserver, long, long)} must be called before * this stream is considered fully initialized. */ public MultiFileOutputStream() { root = new File(System.getProperty("java.io.tmpdir")); namePrefix = yyMMdd_hhmmss() + "." + UUID.randomUUID(); }
MultiFileOutputStream mfos = req.getMultiFileOutputStream(); if (mfos == null) mfos = new MultiFileOutputStream(); try { mfos.init(observer, req.getPartSize(), req.getDiskLimit()); if (defaultExecutorService) mfos.cleanup(); // delete left-over temp files
/** * Deletes files. */ public void cleanup() { for (int i = 0; i < getNumFilesWritten(); i++) { final File f = getFile(i); if (f.exists()) { if (!f.delete()) { LogFactory.getLog(getClass()).debug( "Ignoring failure to delete file " + f); } } } }
/** * Returns the file output stream to be used for writing, blocking if * necessary if running out of disk space. * * @throws InterruptedException if the running thread was interrupted */ private FileOutputStream fos() throws IOException { if (closed) { throw new IOException("Output stream is already closed"); } if (os == null || currFileBytesWritten >= partSize) { if (os != null) { os.close(); // notify about the new file ready for processing observer.onPartCreate(new PartCreationEvent( getFile(filesCreated), filesCreated, false, this)); } currFileBytesWritten = 0; filesCreated++; blockIfNecessary(); final File file = getFile(filesCreated); file.deleteOnExit(); os = new FileOutputStream(file); } return os; }
/** * {@inheritDoc} * * This method would block as necessary if running out of disk space. */ @Override public void write(byte[] b) throws IOException { if (b.length == 0) return; fos().write(b); currFileBytesWritten += b.length; totalBytesWritten += b.length; }
@Override public void close() throws IOException { if (closed) return; closed = true; if (os != null) { os.close(); File lastPart = getFile(filesCreated); if (lastPart.length() == 0) { if (!lastPart.delete()) { LogFactory.getLog(getClass()).debug( "Ignoring failure to delete empty file " + lastPart); } } else { // notify about the new file ready for processing observer.onPartCreate(new PartCreationEvent( getFile(filesCreated), filesCreated, true, this)); } } }
/** * Construct an instance to use the default temporary directory and temp * file naming convention. The * {@link #init(UploadObjectObserver, long, long)} must be called before * this stream is considered fully initialized. */ public MultiFileOutputStream() { root = new File(System.getProperty("java.io.tmpdir")); namePrefix = yyMMdd_hhmmss() + "." + UUID.randomUUID(); }