Skip to content

Instantly share code, notes, and snippets.

@takumakei
Created April 8, 2011 13:07
Show Gist options
  • Save takumakei/909785 to your computer and use it in GitHub Desktop.
Save takumakei/909785 to your computer and use it in GitHub Desktop.
The class has some static methods to use streams easily.
/* IOUtil.java
* The class has some static methods to use streams easily.
*
* Copyright (C) 2011 TAKUMAKei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blogspot.takumakei.gist;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* IO Utility
*/
public class IOUtil {
/**
* Copy all bytes from InputStream to OutputStream.
*/
public static <Output extends OutputStream> Output copy(InputStream input, Output output) throws IOException {
return copy(input, output, 4096);
}
/**
* Copy all bytes from InputStream to OutputStream.
*/
public static <Output extends OutputStream> Output copy(InputStream input, Output output, int bufferSize) throws IOException {
final byte[] buffer = new byte[bufferSize];
for (;;) {
int size = input.read(buffer, 0, bufferSize);
if (-1 == size) {
break;
}
output.write(buffer, 0, size);
}
output.flush();
return output;
}
/**
* Write all bytes to OutputStream.
*/
public static <Output extends OutputStream> Output copy(byte[] input, Output output) throws IOException {
return copy(input, 0, input.length, output);
}
/**
* Write partial bytes to OutputStream.
*/
public static <Output extends OutputStream> Output copy(byte[] input, int off, int len, Output output) throws IOException {
output.write(input, off, len);
return output;
}
/**
* new BufferedInputStream(new FileInputStream(new File(filename)));
*/
public static BufferedInputStream openInputStream(String filename) throws IOException {
return openInputStream(new File(filename));
}
/**
* new BufferedInputStream(new FileInputStream(file))
*/
public static BufferedInputStream openInputStream(File file) throws IOException {
final BufferedInputStream stream =
new BufferedInputStream(
new FileInputStream(file));
return stream;
}
/**
* new BufferedOutputStream(new FileOutputStream(new File(filename)));
*/
public static BufferedOutputStream openOutputStream(String filename) throws IOException {
return openOutputStream(new File(filename));
}
/**
* new BufferedOutputStream(new FileOutputStream(file));
*/
public static BufferedOutputStream openOutputStream(File file) throws IOException {
final BufferedOutputStream stream =
new BufferedOutputStream(
new FileOutputStream(file));
return stream;
}
/**
* Read bytes until EOF.
*/
public static byte[] readAll(InputStream input) throws IOException {
return readAll(input, 4096);
}
public static byte[] readAll(InputStream input, int sizeHint) throws IOException {
return readAll(input, sizeHint, 4096);
}
public static byte[] readAll(InputStream input, int sizeHint, int bufferSize) throws IOException {
return copy(input, new ByteArrayOutputStream(sizeHint), bufferSize).toByteArray();
}
public static byte[] readAll(String filename) throws IOException {
return readAll(new File(filename));
}
public static byte[] readAll(File file) throws IOException {
final InputStream in = openInputStream(file);
try {
return readAll(in);
} finally {
in.close();
}
}
/**
* Write bytes to a file.
*/
public static void writeAll(byte[] input, String filename) throws IOException {
writeAll(input, new File(filename));
}
public static void writeAll(byte[] input, File file) throws IOException {
copy(input, openOutputStream(file)).close();
}
public static void writeAll(byte[] input, int off, int len, String filename) throws IOException {
writeAll(input, off, len, new File(filename));
}
public static void writeAll(byte[] input, int off, int len, File file) throws IOException {
copy(input, off, len, openOutputStream(file)).close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment