Get Stream as String

  • Antworten:0
Mac Systems
  • Forum-Beiträge: 1.727

24.07.2010, 17:25:12 via Website

1public static final int DEFAULT_BUFFER_SIZE = 1024;
2 /**
3 * Reads from given <code>InputStream</code> into a
4 * <code>StringBuffer</code>.
5 *
6 * @param _instream
7 * @return
8 * @throws IOException
9 */
10 public static StringBuilder asString(final InputStream _instream) throws IOException
11 {
12 if (_instream == null)
13 {
14 throw new NullPointerException("InputStream");
15 }
16
17 final BufferedReader reader = new BufferedReader(new InputStreamReader(_instream), 4000);
18
19 try
20 {
21 String line;
22 final StringBuilder builder = new StringBuilder(DEFAULT_BUFFER_SIZE);
23 while ((line = reader.readLine()) != null)
24 {
25 builder.append(line);
26 }
27 return builder;
28
29 }
30 finally
31 {
32 close(reader);
33 }
34 }
35
36
37 /**
38 * closes reader, parameter can be null.
39 *
40 * @param _reader
41 */
42 public final static void close(final Reader _reader)
43 {
44 if (_reader != null)
45 {
46
47 try
48 {
49 _reader.close();
50 }
51 catch (final IOException e)
52 {
53 Log.e(LOG_TAG, "Failed to close stream", e);
54 }
55 }
56 }

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten