Search in sources :

Example 1 with ByteBufferChannel

use of info.ata4.io.buffer.ByteBufferChannel in project disunity by ata4.

the class BundleUtils method byteChannelForEntry.

public static SeekableByteChannel byteChannelForEntry(BundleEntry entry) throws IOException {
    SeekableByteChannel chan;
    // check if the entry is larger than 128 MiB
    long size = entry.size();
    if (size > 1 << 27) {
        // copy entry to temporary file
        Path tmpFile = Files.createTempFile("disunity", null);
        Files.copy(entry.inputStream(), tmpFile, REPLACE_EXISTING);
        chan = Files.newByteChannel(tmpFile, READ, DELETE_ON_CLOSE);
    } else {
        // copy entry to memory
        ByteBuffer bb = ByteBuffer.allocateDirect((int) size);
        IOUtils.copy(entry.inputStream(), new ByteBufferOutputStream(bb));
        bb.flip();
        chan = new ByteBufferChannel(bb);
    }
    return chan;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) Path(java.nio.file.Path) ByteBufferOutputStream(info.ata4.io.buffer.ByteBufferOutputStream) ByteBufferChannel(info.ata4.io.buffer.ByteBufferChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBufferChannel (info.ata4.io.buffer.ByteBufferChannel)1 ByteBufferOutputStream (info.ata4.io.buffer.ByteBufferOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 SeekableByteChannel (java.nio.channels.SeekableByteChannel)1 Path (java.nio.file.Path)1