forked from scribejava/scribejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamUtils.java
More file actions
55 lines (51 loc) · 1.7 KB
/
Copy pathStreamUtils.java
File metadata and controls
55 lines (51 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.github.scribejava.core.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.zip.GZIPInputStream;
/**
* Utils to deal with Streams.
*/
public abstract class StreamUtils {
/**
* Returns the stream contents as an UTF-8 encoded string
*
* @param is input stream
* @return string contents
*/
public static String getStreamContents(InputStream is) {
Preconditions.checkNotNull(is, "Cannot get String from a null object");
try {
final char[] buffer = new char[0x10000];
final StringBuilder out = new StringBuilder();
try (Reader in = new InputStreamReader(is, "UTF-8")) {
int read;
do {
read = in.read(buffer, 0, buffer.length);
if (read > 0) {
out.append(buffer, 0, read);
}
} while (read >= 0);
}
return out.toString();
} catch (IOException ioe) {
throw new IllegalStateException("Error while reading response body", ioe);
}
}
/**
* Return String content from a gzip stream
*
* @param is input stream
* @return string contents
*/
public static String getGzipStreamContents(InputStream is) {
Preconditions.checkNotNull(is, "Cannot get String from a null object");
try {
final GZIPInputStream gis = new GZIPInputStream(is);
return getStreamContents(gis);
} catch (IOException ioe) {
throw new IllegalStateException("Error while reading response body", ioe);
}
}
}