/* CopyUtil.java * * Copyright (c) 2002, Benja Fallenstein * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package org.nongnu.storm.util; import java.io.*; /** Utility methods for copying data from input to output streams. */ public class CopyUtil { static public int copy(InputStream from, OutputStream to) throws IOException { return copy(from, to, 4096); } /** Copy data from an input to an output stream in blocks of a given size. * Closes both streams when the copy operation is complete. */ static public int copy(InputStream from, OutputStream to, int blockSize) throws IOException { try { byte[] buf = new byte[blockSize]; int bytesCopied = 0; //p("start copying"); while(true) { //p("read "); int r = from.read(buf); //p("check("+r+") "); if(r == -1) break; //p("write "); to.write(buf, 0, r); bytesCopied += r; } //p("... all read."); return bytesCopied; } finally { from.close(); to.close(); } } /** Read data from an input stream into a byte array by copying it into * a ByteArrayOutputStream. */ static public byte[] readBytes(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); copy(in, out); return out.toByteArray(); } /** Read data from an input stream into a String. * Most useful for debug output. Encoding is US-ASCII. */ static public String readString(InputStream in) throws IOException { return new String(readBytes(in), "US-ASCII"); } }