Description
Reads in file contents.
License
Open Source License
Parameter
Parameter | Description |
---|
input | The InputStream to read from. |
encoding | The encoding to assume at first. |
Exception
Parameter | Description |
---|
IOException | If the stream cannot be read or the stream cannot be decoded(even) in Latin1 |
Return
A String, interpreted in the "encoding", or, if it fails, in Latin1.
Declaration
public static String readContents(InputStream input, String encoding)
throws IOException
Method Source Code
/*//from ww w. j a v a2 s. c o m
Copyright (c) Inexas 2010
Modifications licensed under the Inexas Software License V1.0. You
may not use this file except in compliance with the License.
The License is available at: http://www.inexas.com/ISL-V1.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
The original file and contents are licensed under a separate license:
see below.
*/
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import org.apache.log4j.Logger;
public class Main{
/** Size of the buffer used when copying large chunks of data. */
private static final int BUFFER_SIZE = 4096;
private static final Logger log = Logger.getLogger(FileUtil.class);
/**
* Reads in file contents.
* <P>
* This method is smart and falls back to ISO-8859-1 if the input stream
* does not seem to be in the specified encoding.
*
* @param input
* The InputStream to read from.
* @param encoding
* The encoding to assume at first.
* @return A String, interpreted in the "encoding", or, if it fails, in
* Latin1.
* @throws IOException
* If the stream cannot be read or the stream cannot be decoded
* (even) in Latin1
*/
public static String readContents(InputStream input, String encoding)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileUtil.copyContents(input, out);
ByteBuffer bbuf = ByteBuffer.wrap(out.toByteArray());
Charset cset = Charset.forName(encoding);
CharsetDecoder csetdecoder = cset.newDecoder();
csetdecoder.onMalformedInput(CodingErrorAction.REPORT);
csetdecoder.onUnmappableCharacter(CodingErrorAction.REPORT);
try {
CharBuffer cbuf = csetdecoder.decode(bbuf);
return cbuf.toString();
} catch (CharacterCodingException e) {
Charset latin1 = Charset.forName("ISO-8859-1");
CharsetDecoder l1decoder = latin1.newDecoder();
l1decoder.onMalformedInput(CodingErrorAction.REPORT);
l1decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
try {
bbuf = ByteBuffer.wrap(out.toByteArray());
CharBuffer cbuf = l1decoder.decode(bbuf);
return cbuf.toString();
} catch (CharacterCodingException ex) {
throw (CharacterCodingException) ex.fillInStackTrace();
}
}
}
/**
* Returns the full contents of the Reader as a String.
*
* @since 1.5.8
* @param in
* The reader from which the contents shall be read.
* @return String read from the Reader
* @throws IOException
* If reading fails.
*/
public static String readContents(Reader in) throws IOException {
Writer out = null;
try {
out = new StringWriter();
copyContents(in, out);
return out.toString();
} finally {
try {
out.close();
} catch (Exception e) {
log.error("Not able to close the stream while reading contents.");
}
}
}
/**
* Just copies all characters from <I>in</I> to <I>out</I>. The copying is
* performed using a buffer of bytes.
*
* @since 1.5.8
* @param in
* The reader to copy from
* @param out
* The reader to copy to
* @throws IOException
* If reading or writing failed.
*/
public static void copyContents(Reader in, Writer out)
throws IOException {
char[] buf = new char[BUFFER_SIZE];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) > 0) {
out.write(buf, 0, bytesRead);
}
out.flush();
}
/**
* Just copies all bytes from <I>in</I> to <I>out</I>. The copying is
* performed using a buffer of bytes.
*
* @since 1.9.31
* @param in
* The inputstream to copy from
* @param out
* The outputstream to copy to
* @throws IOException
* In case reading or writing fails.
*/
public static void copyContents(InputStream in, OutputStream out)
throws IOException {
byte[] buf = new byte[BUFFER_SIZE];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) > 0) {
out.write(buf, 0, bytesRead);
}
out.flush();
}
}
Related
- ReadArrayOfStringPairs( FileInputStream stream)
- ReadInt(FileInputStream stream)
- ReadString(FileInputStream stream)
- readFile(InputStream input)
- readFully(InputStream in)
- readInputStreamToString(InputStream input, String enCoding)
- writeToStream(InputStream in, OutputStream out, boolean closeOnExit)