예제 #1
0
 @Override
 public InputStream createInputStream() throws IOException {
   FileInputStream is = null;
   try {
     is = new FileInputStream(file);
     ByteStreams.skipFully(is, offset);
     return new LimitedInputStream(is, length);
   } catch (IOException e) {
     try {
       if (is != null) {
         long size = file.length();
         throw new IOException(
             "Error in reading " + this + " (actual file length " + size + ")", e);
       }
     } catch (IOException ignored) {
       // ignore
     } finally {
       JavaUtils.closeQuietly(is);
     }
     throw new IOException("Error in opening " + this, e);
   } catch (RuntimeException e) {
     JavaUtils.closeQuietly(is);
     throw e;
   }
 }
예제 #2
0
 @Override
 public ByteBuffer nioByteBuffer() throws IOException {
   FileChannel channel = null;
   try {
     channel = new RandomAccessFile(file, "r").getChannel();
     // Just copy the buffer if it's sufficiently small, as memory mapping has a high overhead.
     if (length < conf.memoryMapBytes()) {
       ByteBuffer buf = ByteBuffer.allocate((int) length);
       channel.position(offset);
       while (buf.remaining() != 0) {
         if (channel.read(buf) == -1) {
           throw new IOException(
               String.format(
                   "Reached EOF before filling buffer\n" + "offset=%s\nfile=%s\nbuf.remaining=%s",
                   offset, file.getAbsoluteFile(), buf.remaining()));
         }
       }
       buf.flip();
       return buf;
     } else {
       return channel.map(FileChannel.MapMode.READ_ONLY, offset, length);
     }
   } catch (IOException e) {
     try {
       if (channel != null) {
         long size = channel.size();
         throw new IOException(
             "Error in reading " + this + " (actual file length " + size + ")", e);
       }
     } catch (IOException ignored) {
       // ignore
     }
     throw new IOException("Error in opening " + this, e);
   } finally {
     JavaUtils.closeQuietly(channel);
   }
 }