Search in sources :

Example 1 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project jetty.project by eclipse.

the class FrameCaptureExtension method saveFrame.

private void saveFrame(Frame frame, boolean outgoing) {
    if (outputDir == null || generator == null) {
        return;
    }
    @SuppressWarnings("resource") SeekableByteChannel channel = (outgoing) ? outgoingChannel : incomingChannel;
    if (channel == null) {
        return;
    }
    ByteBuffer buf = getBufferPool().acquire(BUFSIZE, false);
    try {
        WebSocketFrame f = WebSocketFrame.copy(frame);
        f.setMasked(false);
        generator.generateHeaderBytes(f, buf);
        channel.write(buf);
        if (frame.hasPayload()) {
            channel.write(frame.getPayload().slice());
        }
        if (LOG.isDebugEnabled())
            LOG.debug("Saved {} frame #{}", (outgoing) ? "outgoing" : "incoming", (outgoing) ? outgoingCount.incrementAndGet() : incomingCount.incrementAndGet());
    } catch (IOException e) {
        LOG.warn("Unable to save frame: " + frame, e);
    } finally {
        getBufferPool().release(buf);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project java-chassis by ServiceComb.

the class FortifyUtils method writeFile.

public static void writeFile(String file, byte[] content) throws IOException {
    Set<OpenOption> options = new HashSet<OpenOption>();
    //options.add(StandardOpenOption.CREATE_NEW);
    //options.add(StandardOpenOption.APPEND);
    //覆写文件
    options.add(StandardOpenOption.CREATE);
    options.add(StandardOpenOption.WRITE);
    SeekableByteChannel sbc = null;
    try {
        FileAttribute<?> fa = getDefaultFileAttributes(file);
        ByteBuffer buffer = ByteBuffer.wrap(content);
        sbc = Files.newByteChannel(new File(file).toPath(), options, fa);
        // write data
        sbc.write(buffer);
    } finally {
        IOUtils.closeQuietly(sbc);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) ByteBuffer(java.nio.ByteBuffer) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.

the class TestLeakFS method testLeakByteChannel.

/** Test leaks via Files.newByteChannel */
public void testLeakByteChannel() throws IOException {
    Path dir = wrap(createTempDir());
    OutputStream file = Files.newOutputStream(dir.resolve("stillopen"));
    file.write(5);
    file.close();
    SeekableByteChannel leak = Files.newByteChannel(dir.resolve("stillopen"));
    try {
        dir.getFileSystem().close();
        fail("should have gotten exception");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("file handle leaks"));
    }
    leak.close();
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 4 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.

the class TestVerboseFS method testByteChannel.

/** Test newByteChannel */
public void testByteChannel() throws IOException {
    InfoStreamListener stream = new InfoStreamListener("newByteChannel");
    Path dir = wrap(createTempDir(), stream);
    SeekableByteChannel channel = Files.newByteChannel(dir.resolve("foobar"), StandardOpenOption.CREATE_NEW, StandardOpenOption.READ, StandardOpenOption.WRITE);
    assertTrue(stream.sawMessage());
    channel.close();
    try {
        Files.newByteChannel(dir.resolve("foobar"), StandardOpenOption.CREATE_NEW, StandardOpenOption.READ, StandardOpenOption.WRITE);
        fail("didn't get expected exception");
    } catch (IOException expected) {
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException)

Example 5 with SeekableByteChannel

use of java.nio.channels.SeekableByteChannel in project beam by apache.

the class GcsUtilTest method testGCSChannelCloseIdempotent.

@Test
public void testGCSChannelCloseIdempotent() throws IOException {
    SeekableByteChannel channel = new GoogleCloudStorageReadChannel(null, "dummybucket", "dummyobject", null, new ClientRequestHelper<StorageObject>());
    channel.close();
    channel.close();
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) GoogleCloudStorageReadChannel(com.google.cloud.hadoop.gcsio.GoogleCloudStorageReadChannel) StorageObject(com.google.api.services.storage.model.StorageObject) Test(org.junit.Test)

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