Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to jclouds, Inc. (jclouds) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. jclouds licenses 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 org.jclouds.netty.io;
 
 import java.io.File;
 

Author(s):
Tibor Kiss
 
 public class ChunkedFileInputStream extends InputStream {
 
    private static final int CHUNK_SIZE = 8192;
 
    private ChunkedFile chunks;
    private ChannelBuffer chunk;
 
    private IOException ex;
 
    public ChunkedFileInputStream(String filenamelong offsetlong length) {
       this(new File(filename), offsetlength);
    }
 
    public ChunkedFileInputStream(File filelong offsetlong length) {
       try {
          this. = new ChunkedFile(new RandomAccessFile(file"r"), offsetlength);
       } catch (IOException ex) {
          this. = ex;
       }
    }
 
    private ChannelBuffer getChunk() throws Exception {
       if ( != null) {
          throw ;
       }
       if ( == null) {
           = ChannelBuffer.class.cast(.nextChunk());
       }
       if ( != null) {
          if (.readableBytes() < 1 && .hasNextChunk()) {
              = ChannelBuffer.class.cast(.nextChunk());
             if (.readableBytes() < 1) {
                return null;
             }
          }
       } else {
          return null;
       }
       return ;
    }
 
    @Override
    public int read() throws IOException {
       try {
          ChannelBuffer chunk = getChunk();
          if (chunk == null)
             return -1;
          if (chunk.readableBytes() < 1)
             return -1;
          int readIndex = chunk.readerIndex();
          byte abyte = chunk.getByte(readIndex);
          chunk.readerIndex(readIndex + 1);
          return (intabyte;
       } catch (Exception e) {
          throw new IOException(e);
       }
    }
 
    @Override
    public int read(byte[] bint offint lenthrows IOException {
       try {
          ChannelBuffer chunk = getChunk();
          if (chunk == null)
             return -1;
         int readable = chunk.readableBytes();
         if (readable < 1)
            return -1;
         if (readable > len) {
            readable = len;
         }
         int readIndex = chunk.readerIndex();
         chunk.getBytes(readIndexboffreadable);
         chunk.readerIndex(readIndex + readable);
         return readable;
      } catch (Exception e) {
         throw new IOException(e);
      }
   }
   public void close() throws IOException {
      try {
         .close();
      } catch (Exception e) {
         throw new IOException(e);
      }
   }
New to GrepCode? Check out our FAQ X