Package java.nio.channels

Examples of java.nio.channels.ReadableByteChannel


        }
    }

    public void testInvalidInput() throws Exception {
        String s = "stuff";
        ReadableByteChannel channel = new ReadableByteChannelMockup(
                new String[] {s}, "US-ASCII");
        HttpParams params = new BasicHttpParams();
   
        SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, params);
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
View Full Code Here


            ConnState connState = (ConnState) context.getAttribute(
                    CONN_STATE);
            HttpResponse response = conn.getHttpResponse();
            try {

                ReadableByteChannel channel = connState.getOutputChannel();
                long transferred;

                // Test if the encoder is capable of direct transfer from file
                if (this.useFileChannels &&
                        encoder instanceof FileContentEncoder &&
                        channel instanceof FileChannel) {
                    long pos = connState.getOutputCount();
                    transferred = ((FileContentEncoder) encoder).transfer(
                            (FileChannel) channel, pos, Integer.MAX_VALUE);
                } else {
                    ByteBuffer outbuf = connState.getOutbuf();
                    transferred = channel.read(outbuf);
                    if (transferred != -1) {
                        outbuf.flip();
                        encoder.write(outbuf);
                        outbuf.compact();
                    }
                }
                if (transferred == -1) {
                    encoder.complete();
                }
                if (transferred > 0) {
                    connState.incrementOutputCount(transferred);
                }
               
                if (encoder.isCompleted()) {
                    channel.close();
                    if (!this.connStrategy.keepAlive(response, context)) {
                        conn.close();
                    }
                }
           
View Full Code Here

                final ConnState connState,
                final HttpResponse response) throws HttpException, IOException {
           
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                ReadableByteChannel channel;
                if (entity instanceof HttpNIOEntity) {
                    channel = ((HttpNIOEntity) entity).getChannel();
                } else {
                    channel = Channels.newChannel(entity.getContent());
                }
View Full Code Here

            resource = context.getApplication().getResourceHandler().createResource(resourceName, libraryName);
        }

        if (resource != null) {
            if (resource.userAgentNeedsUpdate(context)) {
                ReadableByteChannel resourceChannel = null;
                WritableByteChannel out = null;
                ByteBuffer buf = allocateByteBuffer();
                try {
                    InputStream in = resource.getInputStream();
                    if (in == null) {
                        send404(context, resourceName, libraryName, true);
                        return;
                    }
                    resourceChannel =
                          Channels.newChannel(in);
                    out = Channels.newChannel(extContext.getResponseOutputStream());
                    extContext.setResponseBufferSize(buf.capacity());
                    String contentType = resource.getContentType();
                    if (contentType != null) {
                        extContext.setResponseContentType(resource.getContentType());
                    }
                    handleHeaders(context, resource);

                    int size = 0;
                    for (int thisRead = resourceChannel.read(buf), totalWritten = 0;
                         thisRead != -1;
                         thisRead = resourceChannel.read(buf)) {

                        buf.rewind();
                        buf.limit(thisRead);
                        do {
                            totalWritten += out.write(buf);
                        } while (totalWritten < size);
                        buf.clear();
                        size += thisRead;

                    }

                    if (!extContext.isResponseCommitted()) {
                        extContext.setResponseContentLength(size);
                    }

                } catch (IOException ioe) {
                    send404(context, resourceName, libraryName, ioe, true);
                } finally {
                    if (out != null) {
                        out.close();
                    }
                    if (resourceChannel != null) {
                        resourceChannel.close();
                    }
                }
            } else {
                send304(context);
            }
View Full Code Here

                  // allocate the space for whole file
                  MappedByteBuffer bb = chch.map(FileChannel.MapMode.READ_WRITE, position + length, 0);
                  bb.force();
                  bb = null;

                  ReadableByteChannel bch = Channels.newChannel(new ByteArrayInputStream(this.data));

                  if ((newIndex = (int)position) > 0)
                  {
                     // begin from the existed bytes
                     chch.transferFrom(bch, 0, newIndex < data.length ? newIndex : data.length);
                     bch.close();
                  }

                  // write update data
                  ReadableByteChannel sch = Channels.newChannel(stream);
                  chch.transferFrom(sch, newIndex, length);
                  sch.close();
                  newIndex += length;

                  if (newIndex < data.length)
                     // write the rest of existed data
                     chch.transferFrom(bch, newIndex, data.length - newIndex);

                  bch.close();
               }
               catch (final IOException e)
               {
                  try
                  {
                     chch.close();
                     PrivilegedFileHelper.delete(chf);
                  }
                  catch (Exception e1)
                  {
                     if (LOG.isTraceEnabled())
                     {
                        LOG.trace("An exception occurred: " + e1.getMessage());
                     }
                  }
                  throw new IOException("update error " + e.getMessage())
                  {
                     @Override
                     public Throwable getCause()
                     {
                        return e;
                     }
                  };
               }
               this.spoolFile = chf;
               this.spoolChannel = chch;
               this.data = null;
            }
         }
         else
         {
            MappedByteBuffer bb = spoolChannel.map(FileChannel.MapMode.READ_WRITE, position, length);

            ReadableByteChannel ch = Channels.newChannel(stream);
            ch.read(bb);
            ch.close();

            bb.force();
         }
      }
View Full Code Here

               try
               {
                  chf = SpoolFile.createTempFile("jcrvdedit", null, tempDirectory);
                  chch = new RandomAccessFile(chf, "rw").getChannel();

                  ReadableByteChannel bch = Channels.newChannel(new ByteArrayInputStream(this.data));
                  chch.transferFrom(bch, 0, this.data.length); // get all
                  bch.close();

                  if (chch.size() < size)
                  {
                     // extend length
                     MappedByteBuffer bb = chch.map(FileChannel.MapMode.READ_WRITE, size, 0);
View Full Code Here

         return size;
      }
      else
      {
         // it's user stream (not a file)
         ReadableByteChannel inch = inFile ? ((FileInputStream)in).getChannel() : Channels.newChannel(in);
         WritableByteChannel outch = outFile ? ((FileOutputStream)out).getChannel() : Channels.newChannel(out);

         long size = 0;
         int r = 0;
         ByteBuffer buff = ByteBuffer.allocate(IOBUFFER_SIZE);
         buff.clear();
         while ((r = inch.read(buff)) >= 0)
         {
            buff.flip();

            // copy all
            do
View Full Code Here

        return cal.getTime().getTime();
    }

    public static void fastChannelCopy(final InputStream in, final OutputStream out) throws IOException {

        final ReadableByteChannel src = Channels.newChannel(in);
        final WritableByteChannel dest = Channels.newChannel(out);

        try {
            final ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
            while (src.read(buffer) != -1) {
                buffer.flip();
                dest.write(buffer);
                buffer.compact();
            }
            buffer.flip();
View Full Code Here

            }
        }
    }

    private static void read(SelectionKey k) {
        ReadableByteChannel ch = (ReadableByteChannel) k.channel();
        NioSocketChannel channel = (NioSocketChannel) k.attachment();

        ReceiveBufferSizePredictor predictor =
            channel.getConfig().getReceiveBufferSizePredictor();
        ByteBuffer buf = ByteBuffer.allocate(predictor.nextReceiveBufferSize());

        int ret = 0;
        int readBytes = 0;
        boolean failure = true;
        try {
            while ((ret = ch.read(buf)) > 0) {
                readBytes += ret;
                if (!buf.hasRemaining()) {
                    break;
                }
            }
View Full Code Here

                                     String clientName,
                                     DataEncryptionKey encryptionKey,
                                     IOStreamPair ioStreams)
                                     throws IOException {
   
    ReadableByteChannel ch;
    if (ioStreams.in instanceof SocketInputWrapper) {
      ch = ((SocketInputWrapper)ioStreams.in).getReadableByteChannel();
    } else {
      ch = (ReadableByteChannel) ioStreams.in;
    }
View Full Code Here

TOP

Related Classes of java.nio.channels.ReadableByteChannel

Copyright © 2018 www.massapicom. 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.