Example usage for java.nio.channels WritableByteChannel write

List of usage examples for java.nio.channels WritableByteChannel write

Introduction

In this page you can find the example usage for java.nio.channels WritableByteChannel write.

Prototype

public int write(ByteBuffer src) throws IOException;

Source Link

Document

Writes a sequence of bytes to this channel from the given buffer.

Usage

From source file:schemacrawler.test.utility.TestUtility.java

private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();//from w  ww.  j  a v a 2 s .c o m
        // write to the channel, may block
        dest.write(buffer);
        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing clear()
        buffer.compact();
    }
    // EOF will leave buffer in fill state
    buffer.flip();
    // make sure the buffer is fully drained.
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:com.spectralogic.ds3client.utils.IOUtils.java

public static long copy(final InputStream inputStream, final WritableByteChannel writableByteChannel,
        final int bufferSize, final String objName, final boolean isPutCommand) throws IOException {
    final byte[] buffer = new byte[bufferSize];
    final ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
    int len;/*from w  ww . ja v  a 2s  . c  o  m*/
    long totalBytes = 0;

    final long startTime = PerformanceUtils.getCurrentTime();
    long statusUpdateTime = startTime;

    try {
        while ((len = inputStream.read(buffer)) != -1) {
            totalBytes += len;

            try {
                byteBuffer.position(0);
                byteBuffer.limit(len);
                writableByteChannel.write(byteBuffer);
            } catch (final Throwable t) {
                throw new UnrecoverableIOException(t);
            }

            final long curTime = PerformanceUtils.getCurrentTime();
            if (statusUpdateTime <= curTime) {
                PerformanceUtils.logMbpsStatus(startTime, curTime, totalBytes, objName, isPutCommand);
                statusUpdateTime += 60000D; //Only logs status once a minute
            }
        }
    } catch (final ConnectionClosedException e) {
        LOG.error("Connection closed trying to copy from stream to channel.", e);
    }

    return totalBytes;
}

From source file:com.pavlospt.rxfile.RxFile.java

private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (src.read(buffer) != -1) {
        buffer.flip();/*from  w w  w  .ja  v  a  2  s .  co  m*/
        dest.write(buffer);
        buffer.compact();
    }
    buffer.flip();
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:it.geosolutions.tools.io.file.IOUtils.java

/**
 * Copies the content of the source channel onto the destination channel.
 * /*  w  ww.jav  a  2s. co  m*/
 * @param bufferSize
 *            size of the temp buffer to use for this copy.
 * @param source
 *            the source {@link ReadableByteChannel}.
 * @param destination
 *            the destination {@link WritableByteChannel};.
 * @throws IOException
 *             in case something bad happens.
 */
public static void copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)
        throws IOException {

    Objects.notNull(source, destination);
    if (!source.isOpen() || !destination.isOpen())
        throw new IllegalStateException("Source and destination channels must be open.");

    final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocateDirect(bufferSize);
    while (source.read(buffer) != -1) {
        // prepare the buffer for draining
        buffer.flip();

        // write to destination
        while (buffer.hasRemaining())
            destination.write(buffer);

        // clear
        buffer.clear();

    }

}

From source file:org.alfresco.repo.transfer.HttpClientTransmitterImpl.java

private static void channelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(2 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();//  w w  w  .j  av  a2s.  c o m
        // write to the channel, may block
        dest.write(buffer);

        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing clear()
        buffer.compact();
    }

    // EOF will leave buffer in fill state
    buffer.flip();

    // make sure the buffer is fully drained.
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:org.cloudfoundry.caldecott.server.converter.ByteBufferHttpMessageConverter.java

@Override
protected void writeInternal(ByteBuffer buffer, HttpOutputMessage outputMessage) throws IOException {
    WritableByteChannel channel = Channels.newChannel(outputMessage.getBody());
    channel.write(buffer);
}

From source file:pl.allegro.tech.hermes.consumers.consumer.sender.http.ByteBufferEntity.java

@Override
public void writeTo(OutputStream outstream) throws IOException {
    Args.notNull(outstream, "Output stream");
    WritableByteChannel channel = Channels.newChannel(outstream);
    channel.write(buffer);
    outstream.flush();//from   w w  w .  ja  v a 2s .c  om
}

From source file:info.fetter.rrdclient.GraphCommand.java

@Override
public void execute(OutputStream out) {
    String command = "graph";
    for (String arg : args) {
        command += " " + arg;
    }/*  w  w w  . j a  v  a2 s.  c  om*/
    try {
        ByteBuffer response = sendCommandToServer(command);
        WritableByteChannel channel = Channels.newChannel(out);
        channel.write(response);
        isOutputParsed = false;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:info.fetter.rrdclient.FetchCommand.java

@Override
public void execute(OutputStream out) {
    String command = "fetch " + fileName + " " + consolidationFunction;
    for (String arg : args) {
        command += " " + arg;
    }/*  w w w. j  a  v a 2 s  . c  om*/
    try {
        ByteBuffer response = sendCommandToServer(command);
        WritableByteChannel channel = Channels.newChannel(out);
        channel.write(response);
        isOutputParsed = false;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:rascal.object.source.BlobSource.java

public final void copyTo(WritableByteChannel destination) throws IOException {
    String headerString = String.format("%s %d", GitObjectType.BLOB, getBlobSize());
    byte[] header = ArrayUtils.add(headerString.getBytes(), (byte) 0);
    destination.write(ByteBuffer.wrap(header));
    copyBlobDataTo(destination);//from   ww  w . jav a 2 s .c  o m
}