<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>biotext.org.uk &#187; algorithms</title>
	<atom:link href="http://biotext.org.uk/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://biotext.org.uk</link>
	<description>Not a typewriter</description>
	<lastBuildDate>Sat, 05 Feb 2011 14:18:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>RetriableTask &#8212; a generic wrapper for retrying operations in Java</title>
		<link>http://biotext.org.uk/retriabletask-a-generic-wrapper-for-retrying-operations-in-java/</link>
		<comments>http://biotext.org.uk/retriabletask-a-generic-wrapper-for-retrying-operations-in-java/#comments</comments>
		<pubDate>Wed, 20 May 2009 16:27:38 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Research]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[LJC]]></category>

		<guid isPermaLink="false">http://biotext.org.uk/?p=360</guid>
		<description><![CDATA[A couple of times recently I&#8217;ve needed to write methods that retry up to n times if an error occurs, and surprisingly, couldn&#8217;t find any standard patterns to accomplish this on the web. So I wrote my own. All comments appreciated (there may well be massive holes in my logic but it works so far). [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of times recently I&#8217;ve needed to write methods that retry up to n times if an error occurs, and surprisingly, couldn&#8217;t find any standard patterns to accomplish this on the web. So I wrote my own. All comments appreciated (there may well be massive holes in my logic but it works so far).</p>
<p><span id="more-360"></span></p>
<pre class="brush: java">
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.logging.Logger;

/**
 * This class is a wrapper for a Callable that adds retry functionality.
 * The user supplies an existing Callable, a maximum number of tries,
 * and optionally a Logger to which exceptions will be logged. Calling
 * the call() method of RetriableTask causes the wrapped object&#039;s call()
 * method to be called, and any exceptions thrown from the inner call()
 * will cause the entire inner call() to be repeated from scratch, as
 * long as the maximum number of tries hasn&#039;t been exceeded.
 * InterruptedException and CancellationException are allowed to
 * propogate instead of causing retries, in order to allow cancellation
 * by an executor service etc.
 *
 * @param &lt;T&gt; the return type of the call() method
 */
public class RetriableTask&lt;T&gt; implements Callable&lt;T&gt;
{

    private final Callable&lt;T&gt; _wrappedTask;

    private final int _tries;

    private final Logger _log;

    /**
     * Creates a new RetriableTask around an existing Callable. Supplying
     * zero or a negative number for the tries parameter will allow the
     * task to retry an infinite number of times -- use with caution!
     *
     * @param taskToWrap the Callable to wrap
     * @param tries the max number of tries
     * @param log a Logger to log exceptions to (null == no logging)
     */
    public RetriableTask ( final Callable&lt;T&gt; taskToWrap, final int tries, final Logger log )
    {
        _wrappedTask = taskToWrap;
        _tries = tries;
        _log = log;
    }

    /**
     * Invokes the wrapped Callable&#039;s call method, optionally retrying
     * if an exception occurs. See class documentation for more detail.
     *
     * @return the return value of the wrapped call() method
     */
    public T call() throws Exception
    {
        int triesLeft = _tries;
        while( true )
        {
            try
            {
                return _wrappedTask.call();
            }
            catch( final InterruptedException e )
            {
                // We don&#039;t attempt to retry these
                throw e;
            }
            catch( final CancellationException e )
            {
                // We don&#039;t attempt to retry these either
                throw e;
            }
            catch( final Exception e )
            {
                triesLeft--;

                // Are we allowed to try again?
                if( triesLeft == 0 ) // No -- rethrow
                    throw e;

                // Yes -- log and allow to loop
                if( _log != null )
                    _log.warning( &quot;Caught exception, retrying... Error was: &quot; + e.getMessage() );
            }
        }

    }

}
</pre>
<p><strong>EDIT:</strong> Just to make it clear how I would use this, it works best in the context of an ExecutorService, which manages multi-threading and timeouts for you. So for example, I could create an executor service like so:</p>
<pre class="brush: java">
ExecutorService pool = Executors.newCachedThreadPool();
</pre>
<p>Then create all the tasks I need, and wrap them in RetriableTasks:</p>
<pre class="brush: java">
// Create a task that returns a String
Callable&lt;String&gt; task1 = new Callable&lt;String&gt;()
{
    public String call()
    {
        // Do stuff here
    }
};
// Make it try up to three times
RetriableTask&lt;String&gt; retriable1 =
    new RetriableTask&lt;String&gt;( task1, 3, null );
</pre>
<p>Then add all the RetriableTasks to a Collection, and execute them all on the thread pool:</p>
<pre class="brush: java">
Collection&lt;Callable&lt;String&gt;&gt; tasks =
    new ArrayList&lt;Callable&lt;String&gt;&gt;();
tasks.add( retriable1 );
// TIMEOUT == timeout in seconds
List&lt;Future&lt;String&gt;&gt; results =
    pool.invokeAll( tasks, TIMEOUT, TimeUnit.SECONDS );
</pre>
<p>Finally, iterate through the results list to get all the return values:</p>
<pre class="brush: java">
for( Future&lt;String&gt; result : results )
{
    // Un-retried exceptions will pop out here
    String returnValue = result.get();
}
</pre>
<p>Or use a <a href="http://eng.genius.com/blog/2009/04/29/java-completionservice/">CompletionService</a>.</p>
<p>Obviously there&#8217;s lots of other things you could do within the same framework &#8212; introducing a delay before retrying would be useful in a lot of circumstances, and also you could supply a list of exceptions that would be allowed to propagate. Or only retry on checked exceptions, and automatically bubble unchecked ones. Really it depends on the use case.</p>
<p>Feel free to write an extended version and post it here or link to it :-)</p>
<p>Andrew.</p>
]]></content:encoded>
			<wfw:commentRss>http://biotext.org.uk/retriabletask-a-generic-wrapper-for-retrying-operations-in-java/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>IteratorReader &#8212; streaming character data from an iterator</title>
		<link>http://biotext.org.uk/iteratorreader-streaming-character-data-from-an-iterator/</link>
		<comments>http://biotext.org.uk/iteratorreader-streaming-character-data-from-an-iterator/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 11:10:02 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Research]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[LJC]]></category>

		<guid isPermaLink="false">http://biotext.org.uk/?p=146</guid>
		<description><![CDATA[Recently when plugging two components of a high-throughput web service together, I ran into a snag. One component (a data repository) exposes an Iterator for pulling XML-formatted records out of it one by one. The other (for serving SOAP response documents) needed, ideally, something that could be wrapped in a StreamSource &#8212; i.e. an InputStream [...]]]></description>
			<content:encoded><![CDATA[<p>Recently when plugging two components of a <a title="FuncNet" href="http://funcnet.eu/">high-throughput web service</a> together, I ran into a snag. One component (a data repository) exposes an Iterator for pulling XML-formatted records out of it one by one. The other (for serving SOAP response documents) needed, ideally, something that could be wrapped in a StreamSource &#8212; i.e. an InputStream or Reader. But although these are both pull-based ways of providing (in this case) character data, they&#8217;re not compatible.</p>
<p>One easy option is to iterate over the whole Iterator and buffer the results in a String, and then use a StringReader. But that&#8217;s not terribly efficient, when you might well be dealing with XML documents in the 10-20MB range. So I wrote an IteratorReader class, which is a Reader that can be wrapped around any Iterator. Each time it&#8217;s read from, it pulls enough elements from the Iterator to enable the request to be fulfilled, and buffers any remainder. This keeps its memory usage down, although this of course depends on (a) the number of characters requested at once from its read method, and (b) the size of the elements coming off the Iterator. (Each element is simply converted into a String via its toString method before being stored in a character buffer.)</p>
<p>Surprisingly, given the vast amount of Java source out there, I couldn&#8217;t find an existing solution for this &#8212; not even in the usually comprehensive <a title="Apache Commons" href="http://commons.apache.org/">Apache Commons</a>. The code is below, and you are free to do what you like with it, but a credit would be nice if you use it, and if you come up with any improvements I&#8217;d be interested to hear about them. In particular, I&#8217;m sure it could be optimized more, as it spends a lot of time garbage collecting in its current form. It&#8217;s pretty thoroughly tested, with an ArrayList of two million random strings as the source of the Iterator, and seems to work fine both with single-character reads and a BufferedReader wrapped round it. Actually, testing taught me some very interesting lessons, but that&#8217;s another post.</p>
<p><span id="more-146"></span></p>
<p><strong>Implementation note:</strong> As well as providing the Iterator to read from, you can optionally provide an object that implements the Closeable interface. This is because in the scenario I developed this for, the Iterator in question represented a stream of objects that was being generated on-the-fly from a live database connection, and implemented Closeable as well as Iterator so the connection could be closed when necessary. I needed a way of doing this automatically from the Reader&#8217;s point of view, so when the Iterator runs out of data (hasNext returns false) the close method of the attached Closeable, if present, is called.</p>
<p>Download the file: <a href="http://biotext.org.uk/static/IteratorReader.java.v0_1">IteratorReader.java.v0_1</a></p>
<p>All comments very gratefully received.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * IteratorReader v. 0.1
 * Andrew B. Clegg
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Closeable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Reader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IteratorReader <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Reader</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// The iterator from which we'll read</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Iterator<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> Object<span style="color: #339933;">&gt;</span> _iterator<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Optionally, an object to close when we're done</span>
    <span style="color: #000000; font-weight: bold;">private</span> Closeable _closeable<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Buffer to hold character pulled from iterator before they're read</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> _leftoverCharsFromLastRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Flag to indicate when iterator is out of elements</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> _iteratorExhausted <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Creates a new IteratorReader.
     * @param iterator the Iterator to read from
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> IteratorReader<span style="color: #009900;">&#40;</span> Iterator<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> Object<span style="color: #339933;">&gt;</span> iterator <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        _iterator <span style="color: #339933;">=</span> iterator<span style="color: #339933;">;</span>
        _closeable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Creates a new IteratorReader whose Iterator is backed by a Closeable
     * object that must be cleanly closed when no longer needed.
     * @param iterator the Iterator to read from
     * @param closeable the Closeable object backing the Iterator
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> IteratorReader<span style="color: #009900;">&#40;</span> Iterator<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> Object<span style="color: #339933;">&gt;</span> iterator, Closeable closeable <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        _iterator <span style="color: #339933;">=</span> iterator<span style="color: #339933;">;</span>
        _closeable <span style="color: #339933;">=</span> closeable<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Closes the Closeable object on which this reader's Iterator depends.
     * If there is no such Closeable, or it has already been closed, this
     * method does nothing. This method is automatically called when Iterator's
     * hasNext method returns false, but can be called earlier.
     * @throws IOException if the Closeable encounters a problem when closing
     */</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> _closeable <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            _closeable.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            _closeable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Reads characters into a portion of an array. See Reader.
     * @param outBuf array to copy the characters into
     * @param outBufOffset offset at which to start storing characters
     * @param charsRequested maximum number of characters to read
     * @return the number of characters read, or -1 if the end of the iterator has been reached
     * @throws IOException if the Closeable encounters a problem when closing
     */</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">int</span> read<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> outBuf, <span style="color: #000066; font-weight: bold;">int</span> outBufOffset, <span style="color: #000066; font-weight: bold;">int</span> charsRequested <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//        System.out.format( &quot;read called: outBufOffset=%d, charsRequested=%d\n&quot;, outBufOffset, charsRequested );</span>
<span style="color: #666666; font-style: italic;">//        System.out.format( &quot;current state: _leftoverCharsFromLastRead has %d characters, _iteratorExhausted=%b\n&quot;,</span>
<span style="color: #666666; font-style: italic;">//                _leftoverCharsFromLastRead.length, _iteratorExhausted );</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Have we already read enough characters from the iterator to feed this request?</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> charsRequested <span style="color: #339933;">&lt;=</span> _leftoverCharsFromLastRead.<span style="color: #006633;">length</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Yes, we already have enough characters, copy them into output buffer</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">arraycopy</span><span style="color: #009900;">&#40;</span> _leftoverCharsFromLastRead, <span style="color: #cc66cc;">0</span>, outBuf, outBufOffset, charsRequested <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// Are there any left over?</span>
            <span style="color: #000066; font-weight: bold;">int</span> remainder <span style="color: #339933;">=</span> _leftoverCharsFromLastRead.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> charsRequested<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">assert</span><span style="color: #009900;">&#40;</span> remainder <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> remainder <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Copy remaining characters to new buffer (i.e. shrink buffer)</span>
                <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> tempBuf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span> remainder <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">arraycopy</span><span style="color: #009900;">&#40;</span> _leftoverCharsFromLastRead, charsRequested, tempBuf, <span style="color: #cc66cc;">0</span>, remainder <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _leftoverCharsFromLastRead <span style="color: #339933;">=</span> tempBuf<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// None left over, so reset buffer to zero-length</span>
                _leftoverCharsFromLastRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// Return the number of characters read</span>
            <span style="color: #666666; font-style: italic;">// (in this case, all the characters requested)</span>
            <span style="color: #000000; font-weight: bold;">return</span> charsRequested<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// We have been asked for more characters than we currently have, so we</span>
            <span style="color: #666666; font-style: italic;">// can return what we have (if there are no more in the iterator) or</span>
            <span style="color: #666666; font-style: italic;">// try to acquire more from the iterator</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// If iterator is exhausted and read has been called again, clean up and</span>
            <span style="color: #666666; font-style: italic;">// return straight away, after copying as many characters as we have left</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> _iteratorExhausted <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">int</span> charsAvailable <span style="color: #339933;">=</span> _leftoverCharsFromLastRead.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> charsAvailable <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// Nothing in the iterator or the buffer, we're done</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// Copy what we have into output buffer</span>
                    <span style="color: #003399;">System</span>.<span style="color: #006633;">arraycopy</span><span style="color: #009900;">&#40;</span> _leftoverCharsFromLastRead, <span style="color: #cc66cc;">0</span>, outBuf, outBufOffset, charsAvailable <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">// Clean up our own buffer and return number of characters copied</span>
                    _leftoverCharsFromLastRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">return</span> charsAvailable<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// There's still data in the iterator, so we can attempt to satisfy the whole request</span>
                <span style="color: #666666; font-style: italic;">// by doing another read -- open a stringbuilder of the desired length</span>
                StringBuilder sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span> charsRequested <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// Insert however many characters we do have and reset our buffer to zero-length</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> _leftoverCharsFromLastRead.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span> _leftoverCharsFromLastRead <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    _leftoverCharsFromLastRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">int</span> charsStillRequired <span style="color: #339933;">=</span> charsRequested <span style="color: #339933;">-</span> _leftoverCharsFromLastRead.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// Iteratively add new strings until no more characters are required</span>
                <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span> charsStillRequired <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>_iteratorExhausted <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// Read another string from the underlying iterator</span>
                    <span style="color: #003399;">String</span> string <span style="color: #339933;">=</span> nextString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">// Add it to stringbuffer</span>
                    sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span> string <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">// Adjust number still required</span>
                    charsStillRequired <span style="color: #339933;">=</span> charsStillRequired <span style="color: #339933;">-</span> string.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #666666; font-style: italic;">// Did we read to the end of the iterator?</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> _iteratorExhausted <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// We have read all the strings from the iterator, but can only return</span>
                    <span style="color: #666666; font-style: italic;">// as many characters as we managed to read, or as many as were requested,</span>
                    <span style="color: #666666; font-style: italic;">// whichever is lower</span>
                    <span style="color: #000066; font-weight: bold;">int</span> charsObtained <span style="color: #339933;">=</span> sb.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> tempBuf <span style="color: #339933;">=</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">// charsToReturn is the number of chars requested, or obtained, whichever is lower</span>
                    <span style="color: #000066; font-weight: bold;">int</span> charsToReturn <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">min</span><span style="color: #009900;">&#40;</span> charsRequested, charsObtained <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">// Copy this many characters into output buffer</span>
                    <span style="color: #003399;">System</span>.<span style="color: #006633;">arraycopy</span><span style="color: #009900;">&#40;</span> tempBuf, <span style="color: #cc66cc;">0</span>, outBuf, outBufOffset, charsToReturn <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">// Do we have any left over in our buffer?</span>
                    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> charsObtained <span style="color: #339933;">&gt;</span> charsRequested <span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">// Yes -- more obtained than requested -- save them for next request</span>
                        <span style="color: #000066; font-weight: bold;">int</span> charsToSave <span style="color: #339933;">=</span> charsObtained <span style="color: #339933;">-</span> charsRequested<span style="color: #339933;">;</span>
                        <span style="color: #000000; font-weight: bold;">assert</span><span style="color: #009900;">&#40;</span> charsToSave <span style="color: #339933;">+</span> charsToReturn <span style="color: #339933;">==</span> tempBuf.<span style="color: #006633;">length</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        _leftoverCharsFromLastRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span> charsToSave <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #003399;">System</span>.<span style="color: #006633;">arraycopy</span><span style="color: #009900;">&#40;</span> tempBuf, charsToReturn, _leftoverCharsFromLastRead, <span style="color: #cc66cc;">0</span>, charsToSave <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> charsObtained <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">// No characters left in buffer or iterator; return -1 immediately</span>
                        _leftoverCharsFromLastRead <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #000000; font-weight: bold;">else</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">// There are some remaining in buffer for next time, so just return</span>
                        <span style="color: #666666; font-style: italic;">// the number we acquired this time</span>
                        <span style="color: #000000; font-weight: bold;">return</span> charsToReturn<span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// sb now contains text to return, and there are more strings to iterate through.</span>
                    <span style="color: #666666; font-style: italic;">// We can save a bit of effort by putting the entire contents of sb into</span>
                    <span style="color: #666666; font-style: italic;">// our 'leftover' characters buffer, and calling this method again to copy it over</span>
                    _leftoverCharsFromLastRead <span style="color: #339933;">=</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">return</span> read<span style="color: #009900;">&#40;</span> outBuf, outBufOffset, charsRequested <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> nextString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// This should never get called after _iteratorExhausted has been set</span>
        <span style="color: #000000; font-weight: bold;">assert</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>_iteratorExhausted <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> _iterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> _iterator.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            _iteratorExhausted <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://biotext.org.uk/iteratorreader-streaming-character-data-from-an-iterator/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

