-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathIOUtils.java
More file actions
221 lines (194 loc) · 6.04 KB
/
Copy pathIOUtils.java
File metadata and controls
221 lines (194 loc) · 6.04 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
public class IOUtils {
public static String toString(InputStream in, Charset charset) throws IOException {
try (in) {
return new String(in.readAllBytes(), charset);
}
}
public static InputStream bounded(InputStream in, long start, long size) throws IOException {
in.skip(start);
return new BoundedInputStream(in, size);
}
// Copy from org.apache.commons.io.input.BoundedInputStream
private static class BoundedInputStream extends InputStream {
private static final int EOF = -1;
/** the wrapped input stream */
private final InputStream in;
/** the max length to provide */
private final long max;
/** the number of bytes already returned */
private long pos;
/** the marked position */
private long mark = EOF;
/** flag if close should be propagated */
private boolean propagateClose = true;
/**
* Creates a new {@code BoundedInputStream} that wraps the given input stream and limits it to a
* certain size.
*
* @param in The wrapped input stream
* @param size The maximum number of bytes to return
*/
public BoundedInputStream(final InputStream in, final long size) {
// Some badly designed methods - eg the servlet API - overload length
// such that "-1" means stream finished
this.max = size;
this.in = in;
}
/**
* Creates a new {@code BoundedInputStream} that wraps the given input stream and is unlimited.
*
* @param in The wrapped input stream
*/
public BoundedInputStream(final InputStream in) {
this(in, EOF);
}
/**
* Invokes the delegate's {@code read()} method if the current position is less than the limit.
*
* @return the byte read or -1 if the end of stream or the limit has been reached.
* @throws IOException if an I/O error occurs.
*/
@Override
public int read() throws IOException {
if (max >= 0 && pos >= max) {
return EOF;
}
final int result = in.read();
pos++;
return result;
}
/**
* Invokes the delegate's {@code read(byte[])} method.
*
* @param b the buffer to read the bytes into
* @return the number of bytes read or -1 if the end of stream or the limit has been reached.
* @throws IOException if an I/O error occurs.
*/
@Override
public int read(final byte[] b) throws IOException {
return this.read(b, 0, b.length);
}
/**
* Invokes the delegate's {@code read(byte[], int, int)} method.
*
* @param b the buffer to read the bytes into
* @param off The start offset
* @param len The number of bytes to read
* @return the number of bytes read or -1 if the end of stream or the limit has been reached.
* @throws IOException if an I/O error occurs.
*/
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
if (max >= 0 && pos >= max) {
return EOF;
}
final long maxRead = max >= 0 ? Math.min(len, max - pos) : len;
final int bytesRead = in.read(b, off, (int) maxRead);
if (bytesRead == EOF) {
return EOF;
}
pos += bytesRead;
return bytesRead;
}
/**
* Invokes the delegate's {@code skip(long)} method.
*
* @param n the number of bytes to skip
* @return the actual number of bytes skipped
* @throws IOException if an I/O error occurs.
*/
@Override
public long skip(final long n) throws IOException {
final long toSkip = max >= 0 ? Math.min(n, max - pos) : n;
final long skippedBytes = in.skip(toSkip);
pos += skippedBytes;
return skippedBytes;
}
/** {@inheritDoc} */
@Override
public int available() throws IOException {
if (max >= 0 && pos >= max) {
return 0;
}
return in.available();
}
/**
* Invokes the delegate's {@code toString()} method.
*
* @return the delegate's {@code toString()}
*/
@Override
public String toString() {
return in.toString();
}
/**
* Invokes the delegate's {@code close()} method if {@link #isPropagateClose()} is {@code true}.
*
* @throws IOException if an I/O error occurs.
*/
@Override
public void close() throws IOException {
if (propagateClose) {
in.close();
}
}
/**
* Invokes the delegate's {@code reset()} method.
*
* @throws IOException if an I/O error occurs.
*/
@Override
public synchronized void reset() throws IOException {
in.reset();
pos = mark;
}
/**
* Invokes the delegate's {@code mark(int)} method.
*
* @param readlimit read ahead limit
*/
@Override
public synchronized void mark(final int readlimit) {
in.mark(readlimit);
mark = pos;
}
/**
* Invokes the delegate's {@code markSupported()} method.
*
* @return true if mark is supported, otherwise false
*/
@Override
public boolean markSupported() {
return in.markSupported();
}
/**
* Indicates whether the {@link #close()} method should propagate to the underling {@link
* InputStream}.
*
* @return {@code true} if calling {@link #close()} propagates to the {@code close()} method of
* the underlying stream or {@code false} if it does not.
*/
public boolean isPropagateClose() {
return propagateClose;
}
/**
* Set whether the {@link #close()} method should propagate to the underling {@link
* InputStream}.
*
* @param propagateClose {@code true} if calling {@link #close()} propagates to the {@code
* close()} method of the underlying stream or {@code false} if it does not.
*/
public void setPropagateClose(final boolean propagateClose) {
this.propagateClose = propagateClose;
}
}
}