Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2015 Santhosh Kumar Tekuri The JLibs authors license this file to you under the Apache License, version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package jlibs.nbp;
 

Author(s):
Santhosh Kumar T
 
 public class NBChannel implements ReadableCharChannel{
     public static final int DEFAULT_BUFFER_SIZE = 8192;
 
     private ReadableByteChannel channel;
     private final ByteBuffer byteBuffer;
     public NBChannel(ReadableByteChannel channelint bufferSize){
          = ByteBuffer.allocate(bufferSize);
         setChannel(channel);        
     }
 
     public NBChannel(ReadableByteChannel channel){
         this(channel);
     }
 
     public ReadableByteChannel getChannel(){
         return ;
     }
 
     public void setChannel(ReadableByteChannel channel){
         this. = channel;
         .clear();
          =  = false;
          = Charset.defaultCharset().name();
          = null;
          = null;
     }
 
     public String fallbackEncoding;
     public String encoding;
 
     public void setEncoding(String encodingboolean fallback){
         if(fallback)
              = encoding;
         else
             this. = encoding;
     }
 
     private CharsetDecoder decoder;
     public CharsetDecoder decoder(){
         return ;
     }
 
     public void decoder(CharsetDecoder decoder){
         this. = decoder;
     }
 
     protected CharsetDecoder createDecoder(ByteBuffer byteBufferboolean eof){
         if(byteBuffer.remaining()>=4 || eof){
             byte marker[] = new byte[Math.min(4, byteBuffer.remaining())];
             for(int i=marker.length-1; i>=0; i--)
                 marker[i] = byteBuffer.get(i);
 
             String encoding = this.;
 
             BOM bom = BOM.get(markertrue);
             if(bom!=null){
                 byteBuffer.position(bom.with().length);
                 if(encoding==null)
                     encoding = bom.encoding();
             }else if(encoding==null){
                 bom = BOM.get(markerfalse);
                 encoding = bom==null ?  : bom.encoding();
             }
             return Charset.forName(encoding).newDecoder();
         }else
             return null;
     }
 
     private boolean eofSeendecode;
 
    @Override
    public int read(CharBuffer charBufferthrows IOException{
        int pos = charBuffer.position();
        while(true){
            if(!){
                if()
                    return -1;
                else{
                    int read=.read();
                    if(read==0)
                        break;
                    else if(read<0)
                         = true;
                     = true;
                    .flip();
                }
            }
            if(==null){
                try{
                    CharsetDecoder decoder = createDecoder();
                    if(decoder!=null)
                        decoder(decoder);
                    else {
                         = false;
                        .position(.limit());
                        .limit(.capacity());
                        continue;
                    }
                }catch(Exception ex){
                    throw new IOException(ex);
                }
            }
            CoderResult cr = .decode(charBuffer);
            if(cr.isOverflow()) // insufficient space in charBuffer
                break;
            else if(cr.isUnderflow()){ // required more bytes
                if(){
                    if(.hasRemaining())
                        break;
                    else{
                        if(charBuffer.position()==pos)
                            return -1;
                         = false;
                        break;
                    }
                }else{
                     = false;
                    .compact();
                    if(!charBuffer.hasRemaining())
                        break;
                }
            }else
                cr.throwException();
        }
        
        return charBuffer.position()-pos;
    }
    @Override
    public boolean isOpen(){
        return !=null;
    }
    @Override
    public void close() throws IOException{
        .close();
         = null;
    }
New to GrepCode? Check out our FAQ X