org.apache.hadoop.hdfs
Class ByteRangeInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.apache.hadoop.fs.FSInputStream
          extended by org.apache.hadoop.hdfs.ByteRangeInputStream
All Implemented Interfaces:
Closeable, org.apache.hadoop.fs.PositionedReadable, org.apache.hadoop.fs.Seekable

public abstract class ByteRangeInputStream
extends org.apache.hadoop.fs.FSInputStream

To support HTTP byte streams, a new connection to an HTTP server needs to be created each time. This class hides the complexity of those multiple connections from the client. Whenever seek() is called, a new connection is made on the successive read(). The normal input stream functions are connected to the currently active input stream.


Nested Class Summary
static class ByteRangeInputStream.URLOpener
          This class wraps a URL and provides method to open connection.
 
Field Summary
protected  long currentPos
           
protected  Long fileLength
           
protected  InputStream in
           
protected  ByteRangeInputStream.URLOpener originalURL
           
protected  ByteRangeInputStream.URLOpener resolvedURL
           
protected  long startPos
           
 
Constructor Summary
ByteRangeInputStream(ByteRangeInputStream.URLOpener o, ByteRangeInputStream.URLOpener r)
          Create with the specified URLOpeners.
 
Method Summary
 void close()
           
protected  InputStream getInputStream()
           
 long getPos()
          Return the current offset from the start of the file
protected abstract  URL getResolvedUrl(HttpURLConnection connection)
           
protected  InputStream openInputStream()
           
 int read()
           
 int read(byte[] b, int off, int len)
           
 void seek(long pos)
          Seek to the given offset from the start of the file.
 boolean seekToNewSource(long targetPos)
          Seeks a different copy of the data.
 
Methods inherited from class org.apache.hadoop.fs.FSInputStream
read, readFully, readFully
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected InputStream in

originalURL

protected ByteRangeInputStream.URLOpener originalURL

resolvedURL

protected ByteRangeInputStream.URLOpener resolvedURL

startPos

protected long startPos

currentPos

protected long currentPos

fileLength

protected Long fileLength
Constructor Detail

ByteRangeInputStream

public ByteRangeInputStream(ByteRangeInputStream.URLOpener o,
                            ByteRangeInputStream.URLOpener r)
Create with the specified URLOpeners. Original url is used to open the stream for the first time. Resolved url is used in subsequent requests.

Parameters:
o - Original url
r - Resolved url
Method Detail

getResolvedUrl

protected abstract URL getResolvedUrl(HttpURLConnection connection)
                               throws IOException
Throws:
IOException

getInputStream

protected InputStream getInputStream()
                              throws IOException
Throws:
IOException

openInputStream

protected InputStream openInputStream()
                               throws IOException
Throws:
IOException

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

seek

public void seek(long pos)
          throws IOException
Seek to the given offset from the start of the file. The next read() will be from that location. Can't seek past the end of the file.

Specified by:
seek in interface org.apache.hadoop.fs.Seekable
Specified by:
seek in class org.apache.hadoop.fs.FSInputStream
Throws:
IOException

getPos

public long getPos()
            throws IOException
Return the current offset from the start of the file

Specified by:
getPos in interface org.apache.hadoop.fs.Seekable
Specified by:
getPos in class org.apache.hadoop.fs.FSInputStream
Throws:
IOException

seekToNewSource

public boolean seekToNewSource(long targetPos)
                        throws IOException
Seeks a different copy of the data. Returns true if found a new source, false otherwise.

Specified by:
seekToNewSource in interface org.apache.hadoop.fs.Seekable
Specified by:
seekToNewSource in class org.apache.hadoop.fs.FSInputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException


Copyright © 2012 Apache Software Foundation. All Rights Reserved.