Examples of ReadableByteChannel


Examples of java.nio.channels.ReadableByteChannel

        log.finer("tcp_recv i_remain == "+i_remain);
      nread = i_remain;
    }

    try {
      ReadableByteChannel rbc = (ReadableByteChannel) fd.channel();
     
      if (rbc instanceof SocketChannel) {
        SocketChannel sc = (SocketChannel) rbc;
        if (sc.isBlocking()) {
          log.fine("SOCKET IS BLOCKING! "+sc);
        }
      } else {
        log.warning("NOT A SOCKET! "+rbc);
      }

      // only read up to nread bytes
      i_buf.limit( Math.min (i_buf.position() + nread, i_buf.capacity()) );
     
      n = rbc.read(i_buf);
      if (log.isLoggable(Level.FINER))
        log.finer("did read " + n + " bytes (ibuf.size="+i_buf.remaining()+")");
      if (n == 0)
        return 0;
     
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

     * @return The request entity if available.
     */
    public Representation getRequestEntity() {
        Representation result = null;
        InputStream requestStream = getRequestStream();
        ReadableByteChannel requestChannel = getRequestChannel();

        if (((requestStream != null) || (requestChannel != null))) {
            // Extract the header values
            MediaType contentMediaType = null;
            long contentLength = Representation.UNKNOWN_SIZE;
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

        return resourceName;
    }

    public static void copyStreamContent(InputStream is, OutputStream os) throws IOException {
        ReadableByteChannel inChannel = Channels.newChannel(is);
        WritableByteChannel outChannel = Channels.newChannel(os);

        // TODO make this configurable
        ByteBuffer buffer = ByteBuffer.allocate(8192);
        int read;

        while ((read = inChannel.read(buffer)) > 0) {
            buffer.rewind();
            buffer.limit(read);

            while (read > 0) {
                read -= outChannel.write(buffer);
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

     *
     * TODO: how to handle partial downloads? Old file is overwritten as soon as FileOutputStream is created.
     */
    public static void downloadToFile (URL url, File file) throws IOException {
        file.getParentFile().mkdirs();
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fos = new FileOutputStream(file);
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        fos.close();
    }
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

   * @throws IOException
   */
  public static void copy(Bucket src, Bucket dst) throws IOException {
    OutputStream out = dst.getOutputStreamUnbuffered();
    InputStream in = src.getInputStreamUnbuffered();
    ReadableByteChannel readChannel = Channels.newChannel(in);
    WritableByteChannel writeChannel = Channels.newChannel(out);
    try {

    // No benefit to allocateDirect() as we're wrapping streams anyway, and worse, it'd be a memory leak.
    ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
    while (readChannel.read(buffer) != -1) {
      buffer.flip();
      while(buffer.hasRemaining())
        writeChannel.write(buffer);
      buffer.clear();
    }

    } finally {
    writeChannel.close();
    readChannel.close();
    }
  }
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

      }
    }
   
    private void processUnwrappedData(byte[] inBuf) throws IOException,
        InterruptedException {
      ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(
          inBuf));
      // Read all RPCs contained in the inBuf, even partial ones
      while (true) {
        int count = -1;
        if (unwrappedDataLengthBuffer.remaining() > 0) {
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

        logger.info("File on disk is same or newer : " + destination);
        return;
      }

      InputStream inputStream = response.getContent();
      ReadableByteChannel rbc = Channels.newChannel(inputStream);
      FileOutputStream fos = new FileOutputStream(destination);
      fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
      fos.close();
     
      destination.setLastModified(date.getTime());
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

    }
    return areTheSame(f, bac.toArray(), dataFile.getPath());
  }
 
  private boolean areTheSame(FileInfo f, final byte[] data, Path p) throws HgInvalidFileException {
    ReadableByteChannel is = null;
    class Check implements ByteChannel {
      final boolean debug = repo.getSessionContext().getLog().isDebug();
      boolean sameSoFar = true;
      int x = 0;

      public int write(ByteBuffer buffer) {
        for (int i = buffer.remaining(); i > 0; i--, x++) {
          if (x >= data.length /*file has been appended*/ || data[x] != buffer.get()) {
            if (debug) {
              byte[] xx = new byte[15];
              if (buffer.position() > 5) {
                buffer.position(buffer.position() - 5);
              }
              buffer.get(xx, 0, min(xx.length, i-1 /*-1 for the one potentially read at buffer.get in if() */));
              String exp;
              if (x < data.length) {
                exp = new String(data, max(0, x - 4), min(data.length - x, 20));
              } else {
                int offset = max(0, x - 4);
                exp = new String(data, offset, min(data.length - offset, 20));
              }
              repo.getSessionContext().getLog().dump(getClass(), Debug, "expected >>%s<< but got >>%s<<", exp, new String(xx));
            }
            sameSoFar = false;
            break;
          }
        }
        buffer.position(buffer.limit()); // mark as read
        return buffer.limit();
      }
     
      public boolean sameSoFar() {
        return sameSoFar;
      }
      public boolean ultimatelyTheSame() {
        return sameSoFar && x == data.length;
      }
    };
    Check check = new Check();
    try {
      is = f.newInputChannel();
//      ByteBuffer fb = ByteBuffer.allocate(min(1 + data.length * 2 /*to fit couple of lines appended; never zero*/, 8192));
      ByteBuffer fb = ByteBuffer.allocate(8192); // FIXME temp fix to ensure big enough buffer for KeywordFilter
      FilterByteChannel filters = new FilterByteChannel(check, repo.getFiltersFromWorkingDirToRepo(p));
      Preview preview = Adaptable.Factory.getAdapter(filters, Preview.class, null);
      if (preview != null) {
        while (is.read(fb) != -1) {
          fb.flip();
          preview.preview(fb);
          fb.clear();
        }
        // reset channel to read once again
        try {
          is.close();
        } catch (IOException ex) {
          repo.getSessionContext().getLog().dump(getClass(), Info, ex, null);
        }
        is = f.newInputChannel();
        fb.clear();
      }
      while (is.read(fb) != -1 && check.sameSoFar()) {
        fb.flip();
        filters.write(fb);
        fb.compact();
      }
      return check.ultimatelyTheSame();
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

      if (LOG.isDebugEnabled()) {
        LOG.debug("Have read input token of size " + inBuf.length
            + " for processing by saslServer.unwrap()");
      }
      inBuf = saslServer.unwrap(inBuf, 0, inBuf.length);
      ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(
          inBuf));
      // Read all RPCs contained in the inBuf, even partial ones
      while (true) {
        int count = -1;
        if (unwrappedDataLengthBuffer.remaining() > 0) {
View Full Code Here

Examples of java.nio.channels.ReadableByteChannel

      }
    }

    private void processUnwrappedData(byte[] inBuf) throws IOException,
    InterruptedException {
      ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(inBuf));
      // Read all RPCs contained in the inBuf, even partial ones
      while (true) {
        int count = -1;
        if (unwrappedDataLengthBuffer.remaining() > 0) {
          count = channelRead(ch, unwrappedDataLengthBuffer);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.