-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathStreamUtils.java
More file actions
189 lines (167 loc) · 5.62 KB
/
StreamUtils.java
File metadata and controls
189 lines (167 loc) · 5.62 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.anddev.andengine.util;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.util.Scanner;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 15:48:56 - 03.09.2009
*/
public class StreamUtils {
// ===========================================================
// Constants
// ===========================================================
public static final int IO_BUFFER_SIZE = 8 * 1024;
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
private StreamUtils() {}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public static final String readFully(final InputStream pInputStream) throws IOException {
final StringBuilder sb = new StringBuilder();
final Scanner sc = new Scanner(pInputStream);
while(sc.hasNextLine()) {
sb.append(sc.nextLine());
}
return sb.toString();
}
public static byte[] streamToBytes(final InputStream pInputStream) throws IOException {
return StreamUtils.streamToBytes(pInputStream, -1);
}
public static byte[] streamToBytes(final InputStream pInputStream, final int pReadLimit) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream((pReadLimit == -1) ? IO_BUFFER_SIZE : pReadLimit);
StreamUtils.copy(pInputStream, os, pReadLimit);
return os.toByteArray();
}
public static void copy(final InputStream pInputStream, final OutputStream pOutputStream) throws IOException {
StreamUtils.copy(pInputStream, pOutputStream, -1);
}
public static void copy(final InputStream pInputStream, final byte[] pData) throws IOException {
int dataOffset = 0;
final byte[] buf = new byte[IO_BUFFER_SIZE];
int read;
while((read = pInputStream.read(buf)) != -1) {
System.arraycopy(buf, 0, pData, dataOffset, read);
dataOffset += read;
}
}
public static void copy(final InputStream pInputStream, final ByteBuffer pByteBuffer) throws IOException {
final byte[] buf = new byte[IO_BUFFER_SIZE];
int read;
while((read = pInputStream.read(buf)) != -1) {
pByteBuffer.put(buf, 0, read);
}
}
/**
* Copy the content of the input stream into the output stream, using a temporary
* byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
*
* @param pInputStream The input stream to copy from.
* @param pOutputStream The output stream to copy to.
* @param pByteLimit not more than so much bytes to read, or unlimited if smaller than 0.
*
* @throws IOException If any error occurs during the copy.
*/
public static void copy(final InputStream pInputStream, final OutputStream pOutputStream, final long pByteLimit) throws IOException {
if(pByteLimit < 0) {
final byte[] buf = new byte[IO_BUFFER_SIZE];
int read;
while((read = pInputStream.read(buf)) != -1) {
pOutputStream.write(buf, 0, read);
}
} else {
final byte[] buf = new byte[IO_BUFFER_SIZE];
final int bufferReadLimit = Math.min((int)pByteLimit, IO_BUFFER_SIZE);
long pBytesLeftToRead = pByteLimit;
int read;
while((read = pInputStream.read(buf, 0, bufferReadLimit)) != -1) {
if(pBytesLeftToRead > read) {
pOutputStream.write(buf, 0, read);
pBytesLeftToRead -= read;
} else {
pOutputStream.write(buf, 0, (int) pBytesLeftToRead);
break;
}
}
}
pOutputStream.flush();
}
public static boolean copyAndClose(final InputStream pInputStream, final OutputStream pOutputStream) {
try {
StreamUtils.copy(pInputStream, pOutputStream, -1);
return true;
} catch (final IOException e) {
return false;
} finally {
StreamUtils.close(pInputStream);
StreamUtils.close(pOutputStream);
}
}
/**
* Closes the specified stream.
*
* @param pCloseable The stream to close.
*/
public static void close(final Closeable pCloseable) {
if(pCloseable != null) {
try {
pCloseable.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
/**
* Flushes and closes the specified stream.
*
* @param pOutputStream The stream to close.
*/
public static void flushCloseStream(final OutputStream pOutputStream) {
if(pOutputStream != null) {
try {
pOutputStream.flush();
} catch (final IOException e) {
e.printStackTrace();
} finally {
StreamUtils.close(pOutputStream);
}
}
}
/**
* Flushes and closes the specified stream.
*
* @param pWriter The Writer to close.
*/
public static void flushCloseWriter(final Writer pWriter) {
if(pWriter != null) {
try {
pWriter.flush();
} catch (final IOException e) {
e.printStackTrace();
} finally {
StreamUtils.close(pWriter);
}
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}