Java-効率的なストリームコピー機
Java An Efficient Stream Copier
public class StreamCopier {
public static void main(String[] args) {
try {
copy(System.in, System.out)
} catch (IOException e) {
System.out.println(e)
}
}
public static void copy(InputStream in, OutputStream out) throws IOException {
/ * Copy process, and no other thread reads the input stream or output stream is written * /
synchronized (in) {
synchronized (out) {
byte[] buffer = new byte[256]
while (true) {
int bytesRead = in.read(buffer)
if (bytesRead == -1) {
break
}
out.write(buffer, 0, bytesRead)
}
}
}
}
}