📄 ioutil.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2001-2005 Aduna * * Contact: * Aduna * Prinses Julianaplein 14 b * 3817 CS Amersfoort * The Netherlands * tel. +33 (0)33 465 99 87 * fax. +33 (0)33 465 99 87 * * http://aduna.biz/ * http://www.openrdf.org/ * * This library 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.1 of the License, or (at your option) any later version. * * This library 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 this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.util.io;import java.io.ByteArrayOutputStream;import java.io.CharArrayWriter;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.Reader;import java.io.Writer;public final class IOUtil { /** * Transfers all bytes that can be read from <tt>in</tt> to <tt>out</tt>. * * @param in The InputStream to read data from. * @param out The OutputStream to write data to. * @return The total number of bytes transfered. **/ public static final int transfer(InputStream in, OutputStream out) throws IOException { int totalBytes = 0; int bytesInBuf = 0; byte[] buf = new byte[1024]; while ((bytesInBuf = in.read(buf)) != -1) { out.write(buf, 0, bytesInBuf); totalBytes += bytesInBuf; } return totalBytes; } /** * Fully reads the bytes available from the supplied InputStream * and returns these bytes in a byte array. * * @param in The InputStream to read the bytes from. * @return A byte array containing the bytes that were read. * @exception IOException If I/O error occurred. **/ public static final byte[] readFully(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(4096); transfer(in, out); out.close(); return out.toByteArray(); } /** * Reads at most <tt>maxBytes</tt> bytes from the supplied input stream and * returns them as a byte array. * * @param in The InputStream supplying the bytes. * @param maxBytes The maximum number of bytes to read from the input stream. * @return A byte array of size <tt>maxBytes</tt> if the input stream can * produce that amount of bytes, or a smaller byte containing all available * bytes from the stream otherwise. **/ public static final byte[] readBytes(InputStream in, int maxBytes) throws IOException { byte[] result = new byte[maxBytes]; int bytesRead = in.read(result); int totalBytesRead = bytesRead; while (totalBytesRead < maxBytes && bytesRead >= 0) { // Read more bytes bytesRead = in.read(result, bytesRead, maxBytes - bytesRead); if (bytesRead > 0) { totalBytesRead += bytesRead; } } if (totalBytesRead < maxBytes) { // Create smaller byte array byte[] tmp = new byte[totalBytesRead]; System.arraycopy(result, 0, tmp, 0, totalBytesRead); result = tmp; } return result; } /** * Writes all bytes from an <tt>InputStream</tt> to a file. * * @param in The <tt>InputStream</tt> containing the data to write * to the file. * @param file The file to write the data to. * @return The total number of bytes written. * @exception IOException If an I/O error occured while trying to write the * data to the file. **/ public static final int writeToFile(InputStream in, File file) throws IOException { FileOutputStream out = new FileOutputStream(file); int totalBytes; try { totalBytes = transfer(in, out); } finally { out.close(); } return totalBytes; } /** * Transfers all characters that can be read from <tt>in</tt> to <tt>out</tt>. * * @param in The Reader to read characters from. * @param out The Writer to write characters to. * @return The total number of characters transfered. **/ public static final int transfer(Reader in, Writer out) throws IOException { int totalChars = 0; int charsInBuf = 0; char[] buf = new char[1024]; while ((charsInBuf = in.read(buf)) != -1) { out.write(buf, 0, charsInBuf); totalChars += charsInBuf; } return totalChars; } /** * Fully reads the characters available from the supplied Reader * and returns these characters as a String object. * * @param reader The Reader to read the characters from. * @return A String existing of the characters that were read. * @exception IOException If I/O error occurred. **/ public static final String readFully(Reader reader) throws IOException { CharArrayWriter out = new CharArrayWriter(4096); transfer(reader, out); out.close(); return out.toString(); } /** * Writes all characters from a <tt>Reader</tt> to a file using the default * character encoding. * * @param reader The <tt>Reader</tt> containing the data to write * to the file. * @param file The file to write the data to. * @return The total number of characters written. * @exception IOException If an I/O error occured while trying to write the * data to the file. * @see java.io.FileWriter **/ public static final int writeToFile(Reader reader, File file) throws IOException { FileWriter writer = new FileWriter(file); int totalChars = 0; try { totalChars = transfer(reader, writer); } finally { writer.close(); } return totalChars; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -