Search in sources :

Example 36 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project Symphony by Vazkii.

the class Mp3File method saveMpegFrames.

private void saveMpegFrames(SeekableByteChannel saveFile) throws IOException {
    int filePos = xingOffset;
    if (filePos < 0)
        filePos = startOffset;
    if (filePos < 0)
        return;
    if (endOffset < filePos)
        return;
    SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, StandardOpenOption.READ);
    ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength);
    try {
        seekableByteChannel.position(filePos);
        while (true) {
            byteBuffer.clear();
            int bytesRead = seekableByteChannel.read(byteBuffer);
            byteBuffer.rewind();
            if (filePos + bytesRead <= endOffset) {
                byteBuffer.limit(bytesRead);
                saveFile.write(byteBuffer);
                filePos += bytesRead;
            } else {
                byteBuffer.limit(endOffset - filePos + 1);
                saveFile.write(byteBuffer);
                break;
            }
        }
    } finally {
        seekableByteChannel.close();
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 37 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project Symphony by Vazkii.

the class Mp3File method init.

private void init(int bufferLength, boolean scanFile) throws IOException, UnsupportedTagException, InvalidDataException {
    if (bufferLength < MINIMUM_BUFFER_LENGTH + 1)
        throw new IllegalArgumentException("Buffer too small");
    this.bufferLength = bufferLength;
    this.scanFile = scanFile;
    try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, StandardOpenOption.READ)) {
        initId3v1Tag(seekableByteChannel);
        scanFile(seekableByteChannel);
        if (startOffset < 0) {
            throw new InvalidDataException("No mpegs frames found");
        }
        initId3v2Tag(seekableByteChannel);
        if (scanFile) {
            initCustomTag(seekableByteChannel);
        }
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel)

Example 38 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project redisson by redisson.

the class RedissonBinaryStreamTest method testChannelPosition.

@Test
public void testChannelPosition() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    SeekableByteChannel c = stream.getChannel();
    c.write(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6, 7 }));
    c.position(3);
    ByteBuffer b = ByteBuffer.allocate(3);
    c.read(b);
    assertThat(c.position()).isEqualTo(6);
    byte[] bb = new byte[3];
    b.flip();
    b.get(bb);
    assertThat(bb).isEqualTo(new byte[] { 4, 5, 6 });
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) RBinaryStream(org.redisson.api.RBinaryStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 39 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project redisson by redisson.

the class RedissonBinaryStreamTest method testChannelTruncate.

@Test
public void testChannelTruncate() throws IOException {
    RBinaryStream stream = redisson.getBinaryStream("test");
    SeekableByteChannel c = stream.getChannel();
    c.write(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6, 7 }));
    assertThat(c.size()).isEqualTo(7);
    c.truncate(3);
    c.position(0);
    c.truncate(10);
    ByteBuffer b = ByteBuffer.allocate(3);
    c.read(b);
    byte[] bb = new byte[3];
    b.flip();
    b.get(bb);
    assertThat(c.size()).isEqualTo(3);
    assertThat(bb).isEqualTo(new byte[] { 1, 2, 3 });
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) RBinaryStream(org.redisson.api.RBinaryStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 40 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project dex2jar by pxb1988.

the class ExtractOdexFromCoredumpCmd method doCommandLine.

@Override
protected void doCommandLine() throws Exception {
    if (remainingArgs.length < 1) {
        throw new HelpException("<core.xxxx> is required.");
    }
    Path core = new File(remainingArgs[0]).toPath();
    try (SeekableByteChannel channel = FileChannel.open(core, StandardOpenOption.READ)) {
        List<Long> possibleOdexs = findPossibleOdexLocation(channel);
        extractDex(channel, possibleOdexs, core.getFileName().toString());
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) File(java.io.File)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)130 ByteBuffer (java.nio.ByteBuffer)58 Path (java.nio.file.Path)48 IOException (java.io.IOException)42 Test (org.junit.Test)33 InputStream (java.io.InputStream)14 NoSuchFileException (java.nio.file.NoSuchFileException)12 Test (org.testng.annotations.Test)9 ReadableByteChannel (java.nio.channels.ReadableByteChannel)8 OpenOption (java.nio.file.OpenOption)7 StandardOpenOption (java.nio.file.StandardOpenOption)7 HashSet (java.util.HashSet)7 File (java.io.File)6 FileSystem (java.nio.file.FileSystem)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 URI (java.net.URI)5 RandomAccessData (org.apache.beam.runners.dataflow.util.RandomAccessData)5 OutputStream (java.io.OutputStream)4 FileChannel (java.nio.channels.FileChannel)4 WritableByteChannel (java.nio.channels.WritableByteChannel)4