01: /*
02: * $Id: IOReader.java,v 1.9 2007/09/18 11:21:09 agoubard Exp $
03: *
04: * Copyright 2003-2007 Orange Nederland Breedband B.V.
05: * See the COPYRIGHT file for redistribution and use restrictions.
06: */
07: package org.xins.common.io;
08:
09: import java.io.BufferedReader;
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.io.InputStreamReader;
13: import java.io.StringWriter;
14:
15: import org.xins.common.MandatoryArgumentChecker;
16:
17: /**
18: * Utilities methods to read input streams.
19: *
20: * @version $Revision: 1.9 $ $Date: 2007/09/18 11:21:09 $
21: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
22: *
23: * @since XINS 2.0.
24: */
25: public final class IOReader {
26:
27: /**
28: * Read an InputStream completly and put the content of the input stream in
29: * a String.
30: *
31: * @param inputStream
32: * the input stream to read, cannot be <code>null</code>.
33: *
34: * @return
35: * the content of the input stream using the default encoding, never <code>null</code>.
36: *
37: * @throws IllegalArgumentException
38: * if <code>inputStream == null</code>.
39: *
40: * @throws IOException
41: * if there are some problems reading the input stream.
42: */
43: public static String readFully(InputStream inputStream)
44: throws IllegalArgumentException, IOException {
45: MandatoryArgumentChecker.check("inputStream", inputStream);
46:
47: BufferedReader input = new BufferedReader(
48: new InputStreamReader(inputStream));
49: StringWriter output = new StringWriter();
50: char[] buffer = new char[1024];
51: while (true) {
52: int length = input.read(buffer);
53: if (length == -1)
54: break;
55: output.write(buffer, 0, length);
56: }
57: inputStream.close();
58: input.close();
59: output.close();
60: return output.toString();
61: }
62: }
|