Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web.connector.grizzly;
 
This class implement IO stream operations on top of a ByteBuffer. Under the hood, this class use a temporary Selector pool to for reading bytes when the client ask for more and the current Selector is not yet ready.

Author(s):
Jeanfrancois Arcand
 
 public class ByteBufferInputStream extends InputStream {
     private static final Logger logger = Logger.getLogger(ByteBufferInputStream.class.getName());
     
     private static int defaultReadTimeout = 15000;
    
    
The wrapped ByteBuffer<
 
     protected ByteBuffer byteBuffer;
    
    
    
The SelectionKey used by this stream.
 
     protected SelectionKey key = null;
    
    
    
The time to wait before timing out when reading bytes
 
     protected int readTimeout = ;
    
    
    
Number of times to retry before return EOF
 
     protected int readTry = 2;
     
     // ------------------------------------------------- Constructor -------//
     
     
     public ByteBufferInputStream() {
     }
     
     
     public ByteBufferInputStream(final ByteBuffer byteBuffer) {
         this. = byteBuffer;
     }
     
     // ---------------------------------------------------------------------//
     
    
    
Set the wrapped ByteBuffer

Parameters:
byteBuffer The wrapped byteBuffer
    public void setByteBuffer(final ByteBuffer byteBuffer) {
        this. = byteBuffer;
    }
    
    
    
Get the wrapped ByteBuffer
    public ByteBuffer getByteBuffer() {
        return  ;
    }
    
    
    
Return the available bytes

Returns:
the wrapped byteBuffer.remaining()
    public int available() {
        return (.remaining());
    }
    
    
    
Close this stream.
    public void close() {
    }
    
    
    
Return true if mark is supported.
    public boolean markSupported() {
        return false;
    }
    
    
    
Read the first byte from the wrapped ByteBuffer.
    public int read() throws IOException {
        if (!.hasRemaining()){
            int eof = 0;
            for (int i=0; i < i++) {
                eof = doRead();
                if ( eof != 0 ){
                    break;
                }
            }
        }
        return (.hasRemaining() ? (.get() & 0xff): -1);
    }
    
    
    
Read the bytes from the wrapped ByteBuffer.
    public int read(byte[] bthrows IOException {
        return (read(b, 0, b.length));
    }
    
    
    
Read the first byte of the wrapped ByteBuffer.
    public int read(byte[] bint offsetint lengththrows IOException {
        if (!.hasRemaining()) {
            int eof = 0;
            for (int i=0; i < i++) {
                eof = doRead();
                
                if ( eof != 0 ){
                    break;
                }
            }
            
            if (eof <= 0){
                return -1;
            }
        }
        
        if (length > .remaining()) {
            length = .remaining();
        }
        .get(boffsetlength);
        
        return (length);
    }
    
    
    
Read the first byte of the wrapped ByteBuffer.
    public int read(ByteBuffer bbthrows IOException {
         = bb;
        int eof = 0;
        for (int i=0; i < i++) {
            eof = doRead();
            if ( eof != 0 ){
                break;
            } 
        }
        if (eof <= 0){
            return -1;
        }
        return .limit();
    }
    
    
    
Recycle this object.
    public void recycle(){
         = null;
         = null;
         = ;
    }
    
    
    
Set the SelectionKey used to reads bytes.
    public void setSelectionKey(SelectionKey key){
        this. = key;
    }

    
    
Read bytes using the read ReadSelector
    protected int doRead() throws IOException{
        if (  == null ) return -1;
        
        .clear();
        try {
            return readBlocking((SocketChannel.channel(), );
        } finally {
            .flip();
        }
    }
    
    
    
Return the timeout between two consecutives Selector.select() when a temporary Selector is used.
    public int getReadTimeout() {
        return ;
    }
    
    
    
Set the timeout between two consecutives Selector.select() when a temporary Selector is used.
    public void setReadTimeout(int rt) {
         = rt;
    }
    
    public static int getDefaultReadTimeout() {
        return ;
    }
    
    public static void setDefaultReadTimeout(int aDefaultReadTimeout) {
         = aDefaultReadTimeout;
    }
    public static int readBlocking(SocketChannel socketChannel,
            ByteBuffer byteBufferint readTimeoutthrows IOException {
        int count = 1;
        int byteRead = 0;
        Selector readSelector = null;
        SelectionKey tmpKey = null;
        Exception exception = null;
        
        try{
            while (count > 0){
                count = socketChannel.read(byteBuffer);
                if (count > -1) {
                    byteRead += count;
                } else if (count == -1 && byteRead == 0) {
                    byteRead = -1;
                }
            }
            if ( byteRead == 0 ){
                readSelector = SelectorFactory.getSelector();
                if ( readSelector == null ){
                    return 0;
                }
                count = 1;
                tmpKey = socketChannel
                        .register(readSelector,.);
                tmpKey.interestOps(tmpKey.interestOps() | .);
                int code = readSelector.select(readTimeout);
                tmpKey.interestOps(
                        tmpKey.interestOps() & (~.));
                if ( code == 0 ){
                    return 0; // Return on the main Selector and try again.
                }
                while (count > 0){
                    count = socketChannel.read(byteBuffer);
                    if (count > -1) {
                        byteRead += count;
                    } else if (count == -1 && byteRead == 0) {
                        byteRead = -1;
                    }
                }
            }
        } catch (Exception e) {
            exception = e;
            
            if (e instanceof IOException) {
                throw (IOExceptione;
            } else {
                throw new IOException(e.getClass().getName() + ": " + e.getMessage());
            }
        } finally {
            if (tmpKey != null)
                tmpKey.cancel();
            if ( readSelector != null){
                // Bug 6403933
                SelectorFactory.selectNowAndReturnSelector(readSelector);
            }
            if (.isLoggable(.)) {
                .log(."InputStream.read [channel=" + socketChannel + ", bytesRead=" + byteRead + ", timeout=" + readTimeout + ", exception=" + exception + "]");
                if (byteRead > 0) {
                    if (.isLoggable(.)) {
                        .log(."InputStream.read-content [channel=" + socketChannel +
                                " content=" + new String(byteBuffer.array(), byteBuffer.arrayOffset() +
                                byteBuffer.position() - byteReadbyteRead) + "]");
                    }
                }
            }
        }
        return byteRead;
    }
New to GrepCode? Check out our FAQ X