Description
Write contents of
buffer
to a temporary file, followed by the remaining bytes in
channel
.
License
Open Source License
Parameter
Parameter | Description |
---|
buffer | contains the contents of the channel read so far |
channel | the rest of the channel |
tempDir | the directory in which to create the temp file |
Exception
Return
File
object for the temporary file.
Declaration
public static File writeEntryToTemp(File tempDir, ByteBuffer buffer, ReadableByteChannel channel)
throws IOException
Method Source Code
//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
public class Main {
/**// w w w. j a va2 s . c o m
*
*/
private static final String TEMP_FILENAME_PREFIX = "droid-archive~";
private static final int WRITE_BUFFER_CAPACITY = 8192;
/**
* Write contents of <code>buffer</code> to a temporary file, followed by the remaining bytes
* in <code>channel</code>.
* <p/>
* <p>The bytes are read from <code>buffer</code> from the current position to its limit.
*
* @param buffer contains the contents of the channel read so far
* @param channel the rest of the channel
* @param tempDir the directory in which to create the temp file
* @return <code>File</code> object for the temporary file.
* @throws java.io.IOException if there is a problem writing to the file
*/
public static File writeEntryToTemp(File tempDir, ByteBuffer buffer, ReadableByteChannel channel)
throws IOException {
final File tempFile = File.createTempFile(TEMP_FILENAME_PREFIX, null, tempDir);
// NEVER use deleteOnExit() for long running processes.
// It can cause the JVM to track the files to delete, which
// is a memory leak for long running processes. Leaving the code and comments in
// here as a warning to any future developers.
// Temporary files created must be deleted by the code requesting the file
// once they are no longer needed.
// DO NOT USE!!!: tempFile.deleteOnExit();
final FileChannel out = (new FileOutputStream(tempFile)).getChannel();
try {
out.write(buffer);
final ByteBuffer buf = ByteBuffer.allocate(WRITE_BUFFER_CAPACITY);
buf.clear();
while (channel.read(buf) >= 0 || buf.position() != 0) {
buf.flip();
out.write(buf);
buf.compact(); // In case of partial write
}
return tempFile;
//CHECKSTYLE:OFF
} catch (RuntimeException ex) {
//CHECKSTYLE:ON
// don't leave temp files lying around if something went wrong.
if (out != null) {
out.close();
}
if (channel != null) {
channel.close();
}
if (tempFile != null) {
tempFile.delete();
}
throw ex;
} finally {
out.close();
}
}
/**
* Write contents of <code>buffer</code> to a temporary file, followed by the remaining bytes
* in <code>channel</code>.
* <p/>
* <p>The bytes are read from <code>buffer</code> from the current position to its limit.
*
* @param tempDir the directory in which to create the temp file
* @param buffer the initial buffer containing the first part of the file.
* @param in the input stream containing the rest of the file to write out.
* @return <code>File</code> object for the temporary file.
* @throws java.io.IOException if there is a problem writing to the file
*/
public static File writeEntryToTemp(File tempDir, byte[] buffer, InputStream in) throws IOException {
final File tempFile = File.createTempFile(TEMP_FILENAME_PREFIX, null, tempDir);
// NEVER use deleteOnExit() for long running processes.
// It can cause the JVM to track the files to delete, which
// is a memory leak for long running processes. Leaving the code and comments in
// here as a warning to any future developers.
// Temporary files created must be deleted by the code requesting the file
// once they are no longer needed.
// DO NOT USE!!!: tempFile.deleteOnExit();
FileOutputStream out = null;
try {
out = new FileOutputStream(tempFile);
final byte[] buf = new byte[WRITE_BUFFER_CAPACITY];
try {
// write the first buffer out:
out.write(buffer);
int bytesRead = in.read(buf);
while (bytesRead > 0) {
out.write(buf, 0, bytesRead);
bytesRead = in.read(buf);
}
return tempFile;
} finally {
if (out != null) {
out.close();
}
}
//CHECKSTYLE:OFF
} catch (RuntimeException ex) {
//CHECKSTYLE:ON
// don't leave temp files lying around if something went wrong.
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (tempFile != null) {
tempFile.delete();
}
throw ex;
}
}
}
Related
- readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer, AtomicBoolean endPointCrashed)
- readToBuffer(ReadableByteChannel src, ByteBuffer dst)
- retryRead(ReadableByteChannel channel, ByteBuffer buffer)
- transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)
- transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)