org.freehep.util.io
Class RoutedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.freehep.util.io.RoutedInputStream
All Implemented Interfaces:
Closeable
Direct Known Subclasses:
PromptInputStream

public class RoutedInputStream
extends InputStream

The RoutedInputStream allows the user to add a listener for a certain delimited portion of the main inputstream. This portion is marked by a start and end marker. The end marker can be null, in which case the portion runs from the start marker to the end of the main inputstream. The listener is informed via a route (partial inputstream) when input is available. The new routed inputstream (route) is supposed to be read to the end or closed, after which the main inputstream should be read again. Closing a route will not close the original stream, but will discard any bytes up to and including the end marker. Returning from a route without reading until the end of the route means that the remaining bytes are still available on the main stream. Multiple routes can be added, as long as they have different start sequences. Start sequences such as "StartA, StartB, StartEmpty" are allowed, but "Start, StartOther" are not since they overlap. Start and End markers can be the same. IMPORTANT: inherits from InputStream rather than FilterInputStream so that the correct read(byte[], int, int) method is used.

Version:
$Id: RoutedInputStream.java 8584 2006-08-10 23:06:37Z duns $
Author:
Mark Donszelmann

Nested Class Summary
 class RoutedInputStream.Route
          Route which can be read up to and including the end marker.
 
Constructor Summary
RoutedInputStream(InputStream input)
          Creates a RoutedInputStream from the underlying stream.
 
Method Summary
 void addRoute(byte[] start, byte[] end, RouteListener listener)
          Adds a route for given start and end marker.
 void addRoute(String start, String end, RouteListener listener)
          Adds a route for given start and end string.
 int read()
          Returns the next byte on this stream, however if a start marker is found, the associated route listener is called, which should take over reading the stream.
 
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RoutedInputStream

public RoutedInputStream(InputStream input)
Creates a RoutedInputStream from the underlying stream.

Parameters:
input - stream to read
Method Detail

read

public int read()
         throws IOException
Returns the next byte on this stream, however if a start marker is found, the associated route listener is called, which should take over reading the stream. In this case this method will, after the route is finished reading and closed, return the first byte after the end marker. This of course unless that byte is part of the next start marker.

Specified by:
read in class InputStream
Throws:
IOException

addRoute

public void addRoute(String start,
                     String end,
                     RouteListener listener)
Adds a route for given start and end string. The strings are converted according to the default encoding to start and end markers (byte[]).

Parameters:
start - start marker
end - end marker
listener - listener to inform about the route

addRoute

public void addRoute(byte[] start,
                     byte[] end,
                     RouteListener listener)
Adds a route for given start and end marker. If the end marker is null, the route is indefinite, and can be read until the main stream ends. If the start and end marker are equal, the route can be read for exactly their length.

Parameters:
start - start marker
end - end marker
listener - listener to inform about the route


Copyright © 2000-2007 FreeHEP. All Rights Reserved.