/***************************************************************************
 *   Copyright (C) 2006-2015 by Fabrizio Montesi <famontesi@gmail.com>     *
 *   Copyright (C) 2015 by Matthias Dieter Wallnöfer                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/


package jolie.net;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class PreBufferedInputStream extends BufferedInputStream
{
	private final static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
	
	public PreBufferedInputStream( InputStream istream )
	{
		super( istream );
	}

	public boolean hasCachedData()
	{
		return pos < count;
	}

	public void append( ByteBuffer b )
		throws IOException
	{
		final int bufferSize = b.remaining();
		enlargeIfNecessary( bufferSize );
		b.get( buf, count, bufferSize );
		count += bufferSize;
	}
	
	private void enlargeIfNecessary( int toBeWritten )
	{
		if ( count + toBeWritten >= buf.length ) {
			int tentative = Math.max( count * 2, count + toBeWritten );
			if ( tentative >= MAX_BUFFER_SIZE ) {
				if ( count + toBeWritten < MAX_BUFFER_SIZE ) {
					tentative = count + toBeWritten;
				} else {
					throw new OutOfMemoryError( "Required array size too large" );
				}
			}
			
			final byte nbuf[] = new byte[tentative];
			System.arraycopy( buf, pos, nbuf, 0, count - pos );
			buf = nbuf;
			pos = 0;
		}
	}
	
	public void append( byte b )
		throws IOException
	{
		if ( buf == null ) {
			throw new IOException( "Stream closed" );
		}
		
		enlargeIfNecessary( 1 );
		buf[ count++ ] = b;
	}
}

@@ -2663,16 +2663,54 @@
 ative%5D;%0A
+%09%09%09final int remaining = count - pos;%0A
 %09%09%09Syste
@@ -2741,27 +2741,25 @@
 buf, 0, 
-count - pos
+remaining
  );%0A%09%09%09b
@@ -2781,16 +2781,38 @@
 os = 0;%0A
+%09%09%09count = remaining;%0A
 %09%09%7D%0A%09%7D%0A%09

Fix to the internal positioning of PreBufferedInputStream