Java tutorial
/* * Copyright (c) 2009 - 2010. School of Information Technology and Electrical * Engineering, The University of Queensland. This software is being developed * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project. * PODD is a National e-Research Architecture Taskforce (NeAT) project * co-funded by ANDS and ARCS. * * PODD is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * PODD 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PODD. If not, see <http://www.gnu.org/licenses/>. */ package podd.util.stream; import org.apache.commons.io.IOUtils; import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import static fedora.server.utilities.StreamUtility.pipeStream; /** * @author Yuan-Fang Li * @version $Id$ */ public class StreamUtility { private static final int BUFFER_LENGTH = 4096; public static File createTempFile(InputStream stream) throws IOException { File tempFile = File.createTempFile("podd-fedora-stream-", Long.toString(System.currentTimeMillis())); try { pipeStream(stream, new FileOutputStream(tempFile), BUFFER_LENGTH); return tempFile; } finally { tempFile.deleteOnExit(); } } public static void closeStream(Closeable closable) throws IOException { if (null != closable) { closable.close(); } } public static String getReaderContent(InputStreamReader reader) throws IOException { final String result; try { result = IOUtils.toString(reader); } finally { IOUtils.closeQuietly(reader); } return result; } public static String getStreamContent(InputStream stream) throws IOException { final String result; try { result = IOUtils.toString(stream); } finally { IOUtils.closeQuietly(stream); } return result; } }