StreamUtils.java
4.35 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.io.file;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class StreamUtils {
private static final Logger log;
public static String convertStreamToString(InputStream in) throws IOException {
log.debug("Starting convertStreamToString (in : " + in + " , " + ")");
return new String(StreamUtils.readBytesFromStream(in));
}
public static byte[] readBytesFromStream(InputStream in) throws IOException {
log.debug("Starting readBytesFromStream (in : " + in + " , " + ")");
int bytesread = 0;
byte[] readBuffer = new byte[4096];
byte[] content = new byte[]{};
while ((bytesread = in.read(readBuffer, 0, readBuffer.length)) > 0) {
content = StreamUtils.append(content, readBuffer, bytesread);
}
in.close();
return content;
}
public static void pipeInputToOutput(InputStream in, OutputStream out) throws IOException {
byte[] readBuffer = new byte[4096];
int bytesread = 0;
while ((bytesread = in.read(readBuffer, 0, readBuffer.length)) > 0) {
out.write(readBuffer, 0, bytesread);
}
in.close();
out.flush();
out.close();
}
public static boolean areStreamsIdentical(InputStream first, InputStream second) throws IOException {
int readFromFirst;
boolean streamsAreIdentical = true;
do {
int readFromSecond;
if ((readFromFirst = first.read()) == (readFromSecond = second.read())) continue;
streamsAreIdentical = false;
break;
} while (readFromFirst != -1);
first.close();
second.close();
return streamsAreIdentical;
}
public static String readStringFromReader(Reader in) throws IOException {
int bytesread = 0;
char[] readBuffer = new char[4096];
char[] content = new char[]{};
while ((bytesread = in.read(readBuffer, 0, readBuffer.length)) > 0) {
content = StreamUtils.append(content, readBuffer, bytesread);
}
in.close();
return new String(content);
}
public static String readStringFromReaderDontClose(Reader in) throws IOException {
int bytesread = 0;
char[] readBuffer = new char[4096];
char[] content = new char[]{};
while ((bytesread = in.read(readBuffer, 0, readBuffer.length)) > 0) {
content = StreamUtils.append(content, readBuffer, bytesread);
}
return new String(content);
}
public static InputStream convertStringToStream(String astring) {
log.debug("Starting convertStringToStream (astring : " + astring + " , " + ")");
return new ByteArrayInputStream(astring.getBytes());
}
public static byte[] append(byte[] source, byte[] addition, int length) {
log.debug("Starting append (source : " + source + " , " + "addition : " + addition + " , " + "length : " + length + " , " + ")");
byte[] dest = new byte[source.length + length];
System.arraycopy(source, 0, dest, 0, source.length);
System.arraycopy(addition, 0, dest, source.length, length);
return dest;
}
public static char[] append(char[] source, char[] addition, int length) {
log.debug("Starting append (source : " + source + " , " + "addition : " + addition + " , " + "length : " + length + " , " + ")");
char[] dest = new char[source.length + length];
System.arraycopy(source, 0, dest, 0, source.length);
System.arraycopy(addition, 0, dest, source.length, length);
return dest;
}
public static String loadFromURL(String urlReference) throws IOException {
int c;
URL pageURL = new URL(urlReference);
URLConnection openConnection = pageURL.openConnection();
openConnection.connect();
InputStream input = openConnection.getInputStream();
StringBuffer buf = new StringBuffer();
while ((c = input.read()) >= 0) {
buf.append((char)c);
}
input.close();
return buf.toString();
}
static {
Class class_ = StreamUtils.class;
log = LoggerFactory.getLogger((Class)class_);
}
}