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 Feeder{
     public static final int DEFAULT_BUFFER_SIZE = 8192;
     
     public final NBParser parser;
     protected ReadableCharChannel channel;
     protected final CharBuffer charBuffer = CharBuffer.allocate();
 
     public Feeder(NBParser parserReadableCharChannel channel){
         this(parser);
         this. = channel;
     }
 
     protected Feeder(NBParser parser){
         this. = parser;
     }
 
     public ReadableCharChannel channel(){
         return ;
     }
 
     public final void setChannel(ReadableCharChannel channel){
          = true;
         this. = channel;
          = null;
         .clear();
     }
 
     public final ReadableByteChannel byteChannel(){
         return  instanceof NBChannel ? ((NBChannel)).getChannel() : null;
     }
 
     protected Feeder child;
     private Feeder parent;
     public final void setChild(Feeder child){
         this. = child;
         child.parent = this;
         . = true;
     }
 
     public final Feeder getParent(){
         return ;
     }
 
     /*-------------------------------------------------[ Eating ]---------------------------------------------------*/
 
     private void closeAll(){
         Feeder feeder = this;
         while(feeder.parent!=null)
             feeder = feeder.parent;
         while(feeder!=null){
             try{
                 if(feeder.channel!=null)
                     feeder.channel.close();
             }catch(IOException ignore){
                 // ignore
             }
             feeder = feeder.child;
         }
     }
 
     public final Feeder feed() throws IOException{
         try{
             try{
                 Feeder current = this;
                 Feeder next;
                 do{
                     next = current.read();
                     if(next==current)
                         return current;
                     current = next;
                 }while(current!=null);
             }catch(CharacterCodingException ex){
                 throw .ioError(ex.getClass().getSimpleName()+": "+ex.getMessage());
            }
            return null;
        }catch(Throwable thr){
            closeAll();
            if(thr instanceof RuntimeException)
                throw (RuntimeException)thr;
            else if(thr instanceof Error)
                throw (Error)thr;
            else
                throw (IOException)thr;
        }
    }
    private boolean readMore = true;
    protected Feeder read() throws IOException{
        final char[] chars = .array();
        if(!=null){
            if(!){
                .position(.consume(chars.position(), .limit(), false));
                if(!=null)
                    return ;
                else{
                    .compact();
                     = true;
                }
            }
            int read;
            while((read=.read())>0){
                .flip();
                .position(.consume(chars.position(), .limit(), false));
                if(!=null){
                     = false;
                    return ;
                }
                .compact();
            }
            if(read==-1){
                .flip();
                .close();
                 = null;
            }
        }
        if(==null){
            boolean canSendEOF = ==null || this.!=.;
            .position(.consume(chars.position(), .limit(), canSendEOF));
            if(!=null)
                return ;
            
            if(!canSendEOF && .hasRemaining())
                    throw new IOException("NotImplemented: remaining "+.position());
            if(!=null)
                . = null;
            return ;
        }else
            return this;
    }
New to GrepCode? Check out our FAQ X