Search in sources :

Example 96 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project jstorm by alibaba.

the class Utils method downloadFromMaster.

public static void downloadFromMaster(Map conf, String file, String localFile) throws IOException, TException {
    WritableByteChannel out = null;
    NimbusClient client = null;
    try {
        client = NimbusClient.getConfiguredClient(conf, 10 * 1000);
        String id = client.getClient().beginFileDownload(file);
        out = Channels.newChannel(new FileOutputStream(localFile));
        while (true) {
            ByteBuffer chunk = client.getClient().downloadChunk(id);
            int written = out.write(chunk);
            if (written == 0) {
                client.getClient().finishFileDownload(id);
                break;
            }
        }
    } finally {
        if (out != null)
            out.close();
        if (client != null)
            client.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 97 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project jstorm by alibaba.

the class ServiceHandler method uploadChunk.

/**
     * uploading topology jar data
     */
@Override
public void uploadChunk(String location, ByteBuffer chunk) throws TException {
    TimeCacheMap<Object, Object> uploaders = data.getUploaders();
    Object obj = uploaders.get(location);
    if (obj == null) {
        throw new TException("File for that location does not exist (or timed out) " + location);
    }
    try {
        if (obj instanceof WritableByteChannel) {
            WritableByteChannel channel = (WritableByteChannel) obj;
            channel.write(chunk);
            uploaders.put(location, channel);
        } else {
            throw new TException("Object isn't WritableByteChannel for " + location);
        }
    } catch (IOException e) {
        String errMsg = " WritableByteChannel write filed when uploadChunk " + location;
        LOG.error(errMsg);
        throw new TException(e);
    }
}
Also used : TException(org.apache.thrift.TException) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException)

Example 98 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project neo4j by neo4j.

the class PageCacheTest method writableByteChannelMustBeOpenUntilClosed.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void writableByteChannelMustBeOpenUntilClosed() throws Exception {
    configureStandardPageCache();
    try (PagedFile pf = pageCache.map(file("a"), filePageSize)) {
        WritableByteChannel channel;
        try (WritableByteChannel ch = pf.openWritableByteChannel()) {
            assertTrue(ch.isOpen());
            channel = ch;
        }
        assertFalse(channel.isOpen());
    }
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) WritableByteChannel(java.nio.channels.WritableByteChannel) Test(org.junit.Test)

Example 99 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project storm by nathanmarz.

the class Utils method downloadFromMaster.

public static void downloadFromMaster(Map conf, String file, String localFile) throws IOException, TException {
    NimbusClient client = NimbusClient.getConfiguredClient(conf);
    String id = client.getClient().beginFileDownload(file);
    WritableByteChannel out = Channels.newChannel(new FileOutputStream(localFile));
    while (true) {
        ByteBuffer chunk = client.getClient().downloadChunk(id);
        int written = out.write(chunk);
        if (written == 0)
            break;
    }
    out.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 100 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project dbeaver by serge-rider.

the class IOUtils method fastCopy.

public static void fastCopy(final InputStream src, final OutputStream dest, int bufferSize) throws IOException {
    final ReadableByteChannel inputChannel = Channels.newChannel(src);
    final WritableByteChannel outputChannel = Channels.newChannel(dest);
    fastCopy(inputChannel, outputChannel, bufferSize);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) WritableByteChannel(java.nio.channels.WritableByteChannel)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)111 ByteBuffer (java.nio.ByteBuffer)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ReadableByteChannel (java.nio.channels.ReadableByteChannel)27 FileOutputStream (java.io.FileOutputStream)23 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Test (org.junit.Test)15 IOException (java.io.IOException)14 File (java.io.File)13 OutputStream (java.io.OutputStream)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)10 Vector (java.util.Vector)10 FileInputStream (java.io.FileInputStream)6 Writer (java.io.Writer)6 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)4 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)4 DbusEventsStatisticsCollector (com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 Map (java.util.Map)4