Java Code Examples for java.nio.channels.ReadableByteChannel#read()

The following examples show how to use java.nio.channels.ReadableByteChannel#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestJSONResponseParser.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Copy all data from <code>in</code> {@link InputStream}
 * into <code>out</code> {@link OutputStream}.
 * <p/>
 * @param in  Source {@link InputStream} to read all data.
 * @param out Target {@link OutputStream} to write all data.
 * @throws IOException when there is a problem with copying data.
 */
public static void copy(InputStream in, OutputStream out) throws IOException {
    try {
        ReadableByteChannel inChannel = Channels.newChannel(in);
        WritableByteChannel outChannel = Channels.newChannel(out);
        ByteBuffer byteBuffer = ByteBuffer.allocate(10240);
        int read;
        do {
            read = inChannel.read(byteBuffer);
            if (read > 0) {
                byteBuffer.limit(byteBuffer.position());
                byteBuffer.rewind();
                outChannel.write(byteBuffer);
                byteBuffer.clear();
            }
        } while (read != -1);
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}
 
Example 2
Source File: BaseLoader.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Load an ndarray from a blob
 *
 * @param blob the blob to load from
 * @return the loaded ndarray
 */
@Override
public INDArray load(Blob blob) throws SQLException {
    if (blob == null)
        return null;
    try(InputStream is = blob.getBinaryStream()) {
        ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
        ReadableByteChannel readableByteChannel = Channels.newChannel(is);
        readableByteChannel.read(direct);
        Buffer byteBuffer = (Buffer) direct;
        byteBuffer.rewind();
        return BinarySerde.toArray(direct);
    } catch (Exception e) {
       throw new RuntimeException(e);
    }


}
 
Example 3
Source File: StorageUtil.java    From archiva with Apache License 2.0 6 votes vote down vote up
public static final void copy( final ReadableByteChannel is, final WritableByteChannel os ) {
    if (is instanceof FileChannel ) {
        copy( (FileChannel) is, os );
    } else if (os instanceof FileChannel) {
        copy(is, (FileChannel)os);
    } else
    {
        try
        {
            ByteBuffer buffer = ByteBuffer.allocate( DEFAULT_BUFFER_SIZE );
            while ( is.read( buffer ) != -1 )
            {
                buffer.flip( );
                while ( buffer.hasRemaining( ) )
                {
                    os.write( buffer );
                }
                buffer.clear( );
            }
        }
        catch ( IOException e )
        {
            throw new RuntimeException( e );
        }
    }
}
 
Example 4
Source File: ContentUtils.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
static long copyChannel(final ReadableByteChannel src, final WritableByteChannel dest, final int bufferSize)
    throws IOException {
    if (src instanceof FileChannel) {
        return copyFileChannel((FileChannel) src, dest, bufferSize);
    }
    long bytes = 0;
    final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();

        // write to the channel, may block
        bytes += 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()) {
        bytes += dest.write(buffer);
    }
    return bytes;
}
 
Example 5
Source File: AsyncProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
static InputStream copyRequestBody(ODataRequest request) {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  InputStream input = request.getBody();
  if (input != null) {
    try {
      ByteBuffer inBuffer = ByteBuffer.allocate(8192);
      ReadableByteChannel ic = Channels.newChannel(input);
      WritableByteChannel oc = Channels.newChannel(buffer);
      while (ic.read(inBuffer) > 0) {
        inBuffer.flip();
        oc.write(inBuffer);
        inBuffer.rewind();
      }
      return new ByteArrayInputStream(buffer.toByteArray());
    } catch (IOException e) {
      throw new ODataRuntimeException("Error on reading request content");
    }
  }
  return null;
}
 
Example 6
Source File: AbstractBox.java    From mp4parser with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@DoNotParseDetail
public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    content = ByteBuffer.allocate(l2i(contentSize));

    while ((content.position() < contentSize)) {
        if (dataSource.read(content) == -1) {
            LOG.error("{} might have been truncated by file end. bytesRead={} contentSize={}", this, content.position(), contentSize);
            break;
        }
    }

    ((Buffer)content).position(0);
    isParsed = false;
}
 
Example 7
Source File: IOUtils.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public static void copy(ReadableByteChannel in, WritableByteChannel out) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
    while (in.read(buffer) != -1) {
        buffer.flip(); // Prepare the buffer to be drained
        while (buffer.hasRemaining()) {
            out.write(buffer);
        }
        buffer.clear(); // Empty buffer to get ready for filling
    }
}
 
Example 8
Source File: DoubleArrayTrie.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Load Stored data
 *
 * @param input  input stream to read the double array trie from
 * @return double array trie, not null
 * @throws IOException if an IO error occured during reading the double array trie
 */
public static DoubleArrayTrie read(InputStream input) throws IOException {
    DoubleArrayTrie trie = new DoubleArrayTrie();
    DataInputStream dis = new DataInputStream(new BufferedInputStream(input));

    trie.compact = dis.readBoolean();
    int baseCheckSize = dis.readInt(); // Read size of baseArr and checkArr
    int tailSize = dis.readInt(); // Read size of tailArr
    ReadableByteChannel channel = Channels.newChannel(dis);

    ByteBuffer tmpBaseBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpBaseBuffer);
    tmpBaseBuffer.rewind();
    trie.baseBuffer = tmpBaseBuffer.asIntBuffer();

    ByteBuffer tmpCheckBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpCheckBuffer);
    tmpCheckBuffer.rewind();
    trie.checkBuffer = tmpCheckBuffer.asIntBuffer();

    ByteBuffer tmpTailBuffer = ByteBuffer.allocate(tailSize * 2);
    channel.read(tmpTailBuffer);
    tmpTailBuffer.rewind();
    trie.tailBuffer = tmpTailBuffer.asCharBuffer();

    input.close();
    return trie;
}
 
Example 9
Source File: ParallelFileProcessing.java    From bazel with Apache License 2.0 5 votes vote down vote up
private boolean readFromChannel(ReadableByteChannel ch, ByteBuffer bb) throws IOException {
  // Continue reading until we filled the minimum buffer size.
  while (bb.position() < parameters.getMinReadBlockSize()) {
    // Stop if we reached the end of stream.
    if (ch.read(bb) < 0) {
      return false;
    }
  }
  return true;
}
 
Example 10
Source File: SimpleCanalConnector.java    From canal with Apache License 2.0 5 votes vote down vote up
private void read(ReadableByteChannel channel, ByteBuffer buffer) throws IOException {
    while (buffer.hasRemaining()) {
        int r = channel.read(buffer);
        if (r == -1) {
            throw new IOException("end of stream when reading header");
        }
    }
}
 
Example 11
Source File: MCP.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void StartInstance(CreateGame createGame, ResponseListener listener) {
  Gdx.app.debug(TAG, "Requesting game instance for " + createGame);
  FlatBufferBuilder builder = new FlatBufferBuilder();
  StartInstance.startStartInstance(builder);
  int startInstanceOffset = StartInstance.endStartInstance(builder);
  int id = MSI.createMSI(builder, MSIData.StartInstance, startInstanceOffset);
  builder.finish(id);
  ByteBuffer data = builder.dataBuffer();

  Socket socket = null;
  try {
    socket = Gdx.net.newClientSocket(Net.Protocol.TCP, "localhost", com.riiablo.server.mcp.MSI.PORT, null);

    OutputStream out = socket.getOutputStream();
    WritableByteChannel channelOut = Channels.newChannel(out);
    channelOut.write(data);

    buffer.clear();
    buffer.mark();
    InputStream in = socket.getInputStream();
    ReadableByteChannel channelIn = Channels.newChannel(in);
    channelIn.read(buffer);
    buffer.limit(buffer.position());
    buffer.reset();

    MSI packet = MSI.getRootAsMSI(buffer);
    Gdx.app.log(TAG, "packet type " + MCPData.name(packet.dataType()));
    listener.handleResponse(packet);
  } catch (Throwable t) {
    listener.failed(t);
  } finally {
    if (socket != null) socket.dispose();
  }
}
 
Example 12
Source File: ChannelHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Channel copy method 1. This method copies data from the src channel and
 * writes it to the dest channel until EOF on src. This implementation makes
 * use of compact( ) on the temp buffer to pack down the data if the buffer
 * wasn't fully drained. This may result in data copying, but minimizes system
 * calls. It also requires a cleanup loop to make sure all the data gets sent.
 * <br>
 * Source: Java NIO, page 60
 *
 * @param aSrc
 *        Source channel. May not be <code>null</code>. Is not closed after
 *        the operation.
 * @param aDest
 *        Destination channel. May not be <code>null</code>. Is not closed
 *        after the operation.
 * @return The number of bytes written.
 */
@Nonnegative
private static long _channelCopy1 (@Nonnull @WillNotClose final ReadableByteChannel aSrc,
                                   @Nonnull @WillNotClose final WritableByteChannel aDest) throws IOException
{
  long nBytesWritten = 0;
  final ByteBuffer aBuffer = ByteBuffer.allocateDirect (16 * 1024);
  while (aSrc.read (aBuffer) != -1)
  {
    // Prepare the buffer to be drained
    aBuffer.flip ();

    // Write to the channel; may block
    nBytesWritten += aDest.write (aBuffer);

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

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

  // Make sure that the buffer is fully drained
  while (aBuffer.hasRemaining ())
    nBytesWritten += aDest.write (aBuffer);

  return nBytesWritten;
}
 
Example 13
Source File: TlsChannelImpl.java    From tls-channel with MIT License 5 votes vote down vote up
public static int readFromChannel(ReadableByteChannel readChannel, ByteBuffer buffer)
    throws IOException, EofException {
  Util.assertTrue(buffer.hasRemaining());
  logger.trace("Reading from channel");
  int c = readChannel.read(buffer); // IO block
  logger.trace("Read from channel; response: {}, buffer: {}", c, buffer);
  if (c == -1) {
    throw new EofException();
  }
  if (c == 0) {
    throw new NeedsReadException();
  }
  return c;
}
 
Example 14
Source File: FreeBox.java    From mp4parser with Apache License 2.0 5 votes vote down vote up
public void parse(ReadableByteChannel dataSource, ByteBuffer header, long contentSize, BoxParser boxParser) throws IOException {
    data = ByteBuffer.allocate(CastUtils.l2i(contentSize));

    int bytesRead = 0;
    int b;
    while (((((b = dataSource.read(data))) + bytesRead) < contentSize)) {
        bytesRead += b;
    }
}
 
Example 15
Source File: FileChannelImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private long transferFromArbitraryChannel(ReadableByteChannel src,
                                          long position, long count)
    throws IOException
{
    // Untrusted target: Use a newly-erased buffer
    int c = (int)Math.min(count, TRANSFER_SIZE);
    ByteBuffer bb = Util.getTemporaryDirectBuffer(c);
    long tw = 0;                    // Total bytes written
    long pos = position;
    try {
        Util.erase(bb);
        while (tw < count) {
            bb.limit((int)Math.min((count - tw), (long)TRANSFER_SIZE));
            // ## Bug: Will block reading src if this channel
            // ##      is asynchronously closed
            int nr = src.read(bb);
            if (nr <= 0)
                break;
            bb.flip();
            int nw = write(bb, pos);
            tw += nw;
            if (nw != nr)
                break;
            pos += nw;
            bb.clear();
        }
        return tw;
    } catch (IOException x) {
        if (tw > 0)
            return tw;
        throw x;
    } finally {
        Util.releaseTemporaryDirectBuffer(bb);
    }
}
 
Example 16
Source File: NioUtils.java    From Mycat-Balance with Apache License 2.0 5 votes vote down vote up
/**
 * 略过通道的所有数据
 * 
 * @param socketChannel
 */
public static int skipChannelData(ReadableByteChannel socketChannel) throws IOException
{
	ByteBuffer buffer = ByteBuffer.allocate(1024);
	int allLength = 0;
	int length = 0;
	while (socketChannel.isOpen() && (length = socketChannel.read(buffer)) > 0)
	{
		allLength += length;
		buffer.clear();
	}
	log.debug("skipped length [{}]", allLength);
	return allLength;
}
 
Example 17
Source File: IO.java    From InflatableDonkey with MIT License 5 votes vote down vote up
@Nonnull
static ByteBuffer readFully(@WillNotClose ReadableByteChannel ch, ByteBuffer bb) throws EOFException, IOException {
    while (bb.hasRemaining()) {
        if (ch.read(bb) == -1) {
            throw new EOFException();
        }
    }
    return bb;
}
 
Example 18
Source File: BufferedSageFile.java    From sagetv with Apache License 2.0 4 votes vote down vote up
public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException
{
  if (readonly)
    throw new IOException("Buffer is read only.");

  if (readLength > 0)
    clearReadBuffer();

  long lastPosition = position();
  int bytesWrite;
  long remaining = count;

  try
  {
    if (position != lastPosition)
      seek(position);

    if (writeWrap == null)
      writeWrap = ByteBuffer.wrap(writeBuffer);

    while (remaining > 0)
    {
      writeWrap.limit((int) Math.min((long) (writeBuffer.length), writeOffset + remaining)).position(writeOffset);
      while (writeWrap.hasRemaining())
      {
        bytesWrite = src.read(writeWrap);

        if (bytesWrite == -1)
          break;

        writeOffset += bytesWrite;
        remaining -= bytesWrite;

        // You must set writePending to true before seeking or the data will be appended to or
        // overwrite the wrong place in the file.
        if (writeOffset == writeBuffer.length)
          flushOptimized();
        else
          writePending = true;
      }
    }
  }
  finally
  {
    seek(lastPosition);
  }

  return count - remaining;
}
 
Example 19
Source File: ByteBufferDeserializer.java    From kieker with Apache License 2.0 4 votes vote down vote up
public void deserialize(final ReadableByteChannel channel) throws IOException {
	final ByteBuffer buffer = this.buffer; // NOCS (hides field)
	while ((channel.read(buffer) != -1)) {
		this.process(buffer);
	}
}
 
Example 20
Source File: StreamCopy.java    From mzmine2 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Copy the data from inputStream to outputStream using nio channels
 * 
 * @param input InputStream
 * @param output OutputStream
 */
public void copy(InputStream input, OutputStream output, long totalLength) throws IOException {

  this.totalLength = totalLength;

  ReadableByteChannel in = Channels.newChannel(input);
  WritableByteChannel out = Channels.newChannel(output);

  // Allocate 1MB buffer
  ByteBuffer bbuffer = ByteBuffer.allocate(1 << 20);

  int len = 0;

  while ((len = in.read(bbuffer)) != -1) {

    if (canceled)
      return;

    bbuffer.flip();
    out.write(bbuffer);
    bbuffer.clear();
    copiedLength += len;
  }

  finished = true;

}