2010. 6. 28. 15:03
InputStream 을 문자열로 반환하는 넘.
InputStream 을 byte array로 반환하는 넘
public static String readStringFromStream(InputStream
in) throws IOException {
StringBuilder sb = new StringBuilder(1024);
for (int i = in.read(); i != -1; i = in.read()) {
sb.append((char) i);
}
in.close();
return sb.toString();
}
StringBuilder sb = new StringBuilder(1024);
for (int i = in.read(); i != -1; i = in.read()) {
sb.append((char) i);
}
in.close();
return sb.toString();
}
InputStream 을 byte array로 반환하는 넘
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
public static byte[] readBytesFromStream(InputStream in) throws IOException {
int i = in.available();
if (i < DEFAULT_BUFFER_SIZE) {
i = DEFAULT_BUFFER_SIZE;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(i);
copy(in, bos);
in.close();
return bos.toByteArray();
}
public static int copy(final InputStream input, final OutputStream output) throws IOException {
return copy(input, output, DEFAULT_BUFFER_SIZE);
}
public static int copy(final InputStream input, final OutputStream output, int bufferSize) throws IOException {
int avail = input.available();
if (avail > 262144) {
avail = 262144;
}
if (avail > bufferSize) {
bufferSize = avail;
}
final byte[] buffer = new byte[bufferSize];
int n = 0;
n = input.read(buffer);
int total = 0;
while (-1 != n) {
output.write(buffer, 0, n);
total += n;
n = input.read(buffer);
}
return total;
}
public static byte[] readBytesFromStream(InputStream in) throws IOException {
int i = in.available();
if (i < DEFAULT_BUFFER_SIZE) {
i = DEFAULT_BUFFER_SIZE;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(i);
copy(in, bos);
in.close();
return bos.toByteArray();
}
public static int copy(final InputStream input, final OutputStream output) throws IOException {
return copy(input, output, DEFAULT_BUFFER_SIZE);
}
public static int copy(final InputStream input, final OutputStream output, int bufferSize) throws IOException {
int avail = input.available();
if (avail > 262144) {
avail = 262144;
}
if (avail > bufferSize) {
bufferSize = avail;
}
final byte[] buffer = new byte[bufferSize];
int n = 0;
n = input.read(buffer);
int total = 0;
while (-1 != n) {
output.write(buffer, 0, n);
total += n;
n = input.read(buffer);
}
return total;
}
'java > util' 카테고리의 다른 글
[XML] - Pretty XML print (0) | 2010.06.28 |
---|---|
[java] - 가변인자(??) (0) | 2010.06.28 |
Calendar, SimpleDateFormat 사용하자 (0) | 2010.06.28 |
[replaceAll] - 문자열 치환 (0) | 2010.06.28 |
[java] NumberFormat 을 사용해서 double 가독성 있게 표현하기 (0) | 2010.06.25 |