/** * Update inner stream with a new stream and position * @param newStream new stream -must not be null * @param newpos new position * @throws IOException IO exception on a failure to close the existing * stream. */ private synchronized void updateInnerStream(InputStream newStream, long newpos) throws IOException { Preconditions.checkNotNull(newStream, "Null newstream argument"); closeInnerStream(); in = newStream; this.pos = newpos; }
@Override public synchronized int read() throws IOException { int result; try { result = in.read(); } catch (IOException e) { LOG.info("Received IOException while reading '{}', attempting to reopen", key); LOG.debug("{}", e, e); try { reopen(pos); result = in.read(); } catch (EOFException eof) { LOG.debug("EOF on input stream read: {}", eof, eof); result = -1; } } if (result != -1) { pos++; } if (statistics != null && result != -1) { statistics.incrementBytesRead(1); } return result; } @Override
/** * Reopen a new input stream with the specified position * @param pos the position to reopen a new stream * @throws IOException */ private synchronized void reopen(long pos) throws IOException { LOG.debug("Reopening key '{}' for reading at position '{}", key, pos); InputStream newStream = store.retrieve(key, pos); updateInnerStream(newStream, pos); }
NativeS3FsInputStream stream = new NativeS3FsInputStream(new MockNativeFileSystemStore(), null, is, ""); result[i] = (byte)stream.read(); int read = stream.read(temp, 0, 8); assertEquals(8, read); System.arraycopy(temp, 0, result, i, 8);
@Override public synchronized int read(byte[] b, int off, int len) throws IOException { if (in == null) { throw new EOFException("Cannot read closed stream"); } int result = -1; try { result = in.read(b, off, len); } catch (EOFException eof) { throw eof; } catch (IOException e) { LOG.info( "Received IOException while reading '{}'," + " attempting to reopen.", key); reopen(pos); result = in.read(b, off, len); } if (result > 0) { pos += result; } if (statistics != null && result > 0) { statistics.incrementBytesRead(result); } return result; }
@Override public FSDataInputStream open(Path f, int bufferSize) throws IOException { if (!exists(f)) { throw new FileNotFoundException(f.toString()); } Path absolutePath = makeAbsolute(f); String key = pathToKey(absolutePath); return new FSDataInputStream(new BufferedFSInputStream( new NativeS3FsInputStream(store.retrieve(key), key), bufferSize)); }
@Override public synchronized void seek(long newpos) throws IOException { if (newpos < 0) { throw new EOFException( FSExceptionMessages.NEGATIVE_SEEK); } if (pos != newpos) { // the seek is attempting to move the current position reopen(newpos); } }
@Override public synchronized void close() throws IOException { closeInnerStream(); }
@Override public FSDataInputStream open(Path f, int bufferSize) throws IOException { FileStatus fs = getFileStatus(f); // will throw if the file doesn't exist if (fs.isDirectory()) { throw new FileNotFoundException("'" + f + "' is a directory"); } LOG.info("Opening '" + f + "' for reading"); Path absolutePath = makeAbsolute(f); String key = pathToKey(absolutePath); return new FSDataInputStream(new BufferedFSInputStream( new NativeS3FsInputStream(store, statistics, store.retrieve(key), key), bufferSize)); }
@Override public FSDataInputStream open(Path f, int bufferSize) throws IOException { if (!exists(f)) { throw new FileNotFoundException(f.toString()); } Path absolutePath = makeAbsolute(f); String key = pathToKey(absolutePath); return new FSDataInputStream(new BufferedFSInputStream( new NativeS3FsInputStream(store.retrieve(key), key), bufferSize)); }