/*\
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
* _____________________________________________________________________________________________________________________
*
* This class is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it
* under the terms of the BSD License. The license text is appended to the end of this file.
\*/
package de.klaro.base.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
/**
* Methoden zur Manipulation und Verarbeitung von Streams.
*
* @see java.io.File
*/
public class Streams extends Object
{
/* ______________________________________________________________________________________________________________ *\
\* Konstanten */
/* ______________________________________________________________________________________________________________ *\
\* Klassenvariablen */
private static byte[] $buffer = new byte[2048];
/* ______________________________________________________________________________________________________________ *\
\* Instanzvariablen */
/* ______________________________________________________________________________________________________________ *\
\* Konstruktoren */
/**
* Erzeugt eine Instanz dieser Klasse.
*
* Da verhindert werden soll, dass Instanzen dieser Klasse ausserhalb dieser Klasse erzeugt werdem, ist dieser
* Konstruktor 'private' deklariert.
*/
private Streams()
{
super();
}
/* ______________________________________________________________________________________________________________ *\
\* Instanzmethoden */
/* ______________________________________________________________________________________________________________ *\
\* Klassenmethoden */
/**
* Kopiert einen Input-Stream blockweise in einen Output-Stream.
*
* Der Block hat einen Gre von 2048 Bytes.
*
* @param source Die Quelle
* @param destination Das Ziel
* @param autoClose Die Streams sollen nach dem Kopieren geschlossen werden
* @return Die Anzahl der kopierten Bytes, oder ein negativer Wert, wenn ein Fehler aufgetreten ist
*/
public synchronized static int copy(InputStream source,OutputStream destination,boolean autoClose)
{
int sum = 0;
try
{
int n;
while ((n = source.read($buffer)) >= 0)
{
sum += n;
destination.write($buffer,0,n);
}
}
catch (Exception e)
{
sum = (sum == 0)? Integer.MIN_VALUE: -sum;
}
finally
{
if (autoClose)
{
closeQuietly(source);
closeQuietly(destination);
}
}
return sum;
}
/**
* Liefert den Inhalt eines Streams.
*
* @param stream Der Stream
* @param autoClose Der Stream sollen nach dem Lesen geschlossen werden
* @return Der Inhalt der Datei
* @throws IOException Die Datei konnte nicht gelesen werden
*/
public static synchronized byte[] getBytes(InputStream stream,boolean autoClose) throws IOException
{
int length = $buffer.length;
byte[] result = new byte[length];
try
{
int offset = 0;
int n;
while ((n = stream.read($buffer)) >= 0)
{
if (length < offset + n)
{
while (length < offset + n)
length <<= 1;
byte[] buffer = new byte[length];
System.arraycopy(result,0,buffer,0,offset);
result = buffer;
buffer = null; // Explicitly inform the garbage collector about this array
}
System.arraycopy($buffer,0,result,offset,n);
offset += n;
}
if (offset < length)
{
byte[] buffer = new byte[offset];
System.arraycopy(result,0,buffer,0,offset);
result = buffer;
buffer = null; // Explicitly inform the garbage collector about this array
}
return result;
}
finally
{
if (autoClose)
Streams.closeQuietly(stream);
}
}
/**
* Liefert den Inhalt eines Streams als String.
*
* @param stream Der Stream
* @param autoClose Der Stream sollen nach dem Lesen geschlossen werden
* @return Der Inhalt der Datei als String
* @throws IOException Die Datei konnte nicht gelesen werden
*/
public static String getContent(InputStream stream,boolean autoClose) throws IOException
{
return new String(getBytes(stream,autoClose));
}
/**
* Schliesst einen Input-Stream, ohne dass I/O-Fehler bercksichtigt werden.
*
* @param inputStream Der Input-Stream
*/
public static void closeQuietly(InputStream inputStream)
{
try
{
if (inputStream != null)
inputStream.close();
}
catch (IOException e)
{
// Keine Aktion ntig
}
}
/**
* Schliesst einen Output-Stream, ohne dass I/O-Fehler bercksichtigt werden.
*
* @param outputStream Der Output-Stream
*/
public static void closeQuietly(OutputStream outputStream)
{
try
{
if (outputStream != null)
outputStream.close();
}
catch (IOException e)
{
// Keine Aktion ntig
}
}
/**
* Schliesst einen Reader, ohne dass I/O-Fehler bercksichtigt werden.
*
* @param reader Der Reader
*/
public static void closeQuietly(Reader reader)
{
try
{
if (reader != null)
reader.close();
}
catch (IOException e)
{
// Keine Aktion ntig
}
}
/**
* Schliesst einen Writer, ohne dass I/O-Fehler bercksichtigt werden.
*
* @param writer Der Writer
*/
public static void closeQuietly(Writer writer)
{
try
{
if (writer != null)
writer.close();
}
catch (IOException e)
{
// Keine Aktion ntig
}
}
/* ______________________________________________________________________________________________________________ *\
\* Klassen */
}
/*\
* _____________________________________________________________________________________________________________________
*
* This software is distributed under the terms of the BSD License:
*
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of the Klaus Rogall nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* _____________________________________________________________________________________________________________________
\*/