edu.mit.jmwe.util
Class StreamAdapter

java.lang.Object
  extended by java.io.InputStream
      extended by edu.mit.jmwe.util.StreamAdapter
All Implemented Interfaces:
Closeable

public class StreamAdapter
extends InputStream

An adapter that makes a ByteBuffer look like an InputStream.

Since:
jMWE 1.0.0
Version:
$Id: StreamAdapter.java 611 2011-05-06 20:16:56Z markaf $
Author:
M.A. Finlayson

Field Summary
static String FILE_PROTOCOL
          The string that indicates the file protocol.
static String UTF8
          The string that indicates the UTF8 character encoding.
 
Constructor Summary
StreamAdapter(ByteBuffer buffer)
          Creates a new instance of this class that wraps the specified buffer as an InputStream.
 
Method Summary
 int available()
           
 void close()
           
static InputStream make(File file)
          Returns an input stream on the specified file that is backed by a mapped byte buffer, making it much faster than a normal FileInputStream.
static InputStream make(URL url)
          Returns an input stream for the specified URL.
 void mark(int readlimit)
           
 boolean markSupported()
           
 int read()
           
 void reset()
           
static File toFile(URL url)
          Transforms the specified URL into a File object, if in fact the URL points to a file.
static URL toURL(File file)
          Transforms a file into a URL
 
Methods inherited from class java.io.InputStream
read, read, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_PROTOCOL

public static final String FILE_PROTOCOL
The string that indicates the file protocol.

Since:
jMWE 1.0.0
See Also:
Constant Field Values

UTF8

public static final String UTF8
The string that indicates the UTF8 character encoding.

Since:
jMWE 1.0.0
See Also:
Constant Field Values
Constructor Detail

StreamAdapter

public StreamAdapter(ByteBuffer buffer)
Creates a new instance of this class that wraps the specified buffer as an InputStream.

Throws:
NullPointerException - if the specified buffer is null
Since:
jMWE 1.0.0
Method Detail

available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

read

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

close

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

mark

public void mark(int readlimit)
Overrides:
mark in class InputStream

reset

public void reset()
           throws IOException
Overrides:
reset in class InputStream
Throws:
IOException

markSupported

public boolean markSupported()
Overrides:
markSupported in class InputStream

toFile

public static File toFile(URL url)
Transforms the specified URL into a File object, if in fact the URL points to a file. That is, if the URL uses the file:// protocol and is in UTF8 encoding. If the transformation cannot be done, this method returns null.

Throws:
NullPointerException - if the specified URL is null
Since:
jMWE 1.0.0

toURL

public static URL toURL(File file)
Transforms a file into a URL

Parameters:
file - the file to be transformed
Returns:
a URL representing the file
Throws:
NullPointerException - if the specified file is null
Since:
jMWE 1.0.0

make

public static InputStream make(URL url)
                        throws IOException
Returns an input stream for the specified URL. This implementation produces a mapped byte-buffer wrapped as a stream if the specified URL points to a local file system resource.

Throws:
NullPointerException - if the specified URL is null
IOException
Since:
jMWE 1.0.0

make

public static InputStream make(File file)
                        throws IOException
Returns an input stream on the specified file that is backed by a mapped byte buffer, making it much faster than a normal FileInputStream.

Parameters:
file - the file on which to open the stream
Returns:
the mapped-byte-buffer-backed input stream for reading the file
Throws:
IOException - if there was a io error accessing the file
Since:
jMWE 1.0.0


Copyright © 2011 Massachusetts Institute of Technology. All Rights Reserved.