All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nervousync.utils.IOUtils Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.nervousync.utils;

import java.io.*;

import org.nervousync.commons.Globals;

/**
 * 

Input/Output Utilities

*

输入/输出工具集

* * @author Steven Wee [email protected] * @version $Revision: 1.2.0 $ $Date: Jun 3, 2015 11:20:20 $ */ public final class IOUtils { /** * Logger instance * 日志实例 */ private static final LoggerUtils.Logger LOGGER = LoggerUtils.getLogger(IOUtils.class); /** *

Private constructor for IOUtils

*

输入/输出工具集的私有构造方法

*/ private IOUtils() { } /** *

Read data bytes from given input stream instance

*

从给定的输入流实例对象中读取字节数组

* * @param inputStream input stream instance * 输入流实例对象 * * @return Read data bytes, or zero length byte array if an error occurs * 读取的字节数组,如果读取过程中出现异常则返回长度为0的字节数组 */ public static byte[] readBytes(final InputStream inputStream) { return readBytes(inputStream, Globals.DEFAULT_VALUE_INT, Globals.DEFAULT_VALUE_INT); } /** *

Read data bytes from given input stream instance

*

从给定的输入流实例对象中读取字节数组

* * @param inputStream input stream instance * 输入流实例对象 * @param offset read offset * 读取起始偏移量 * @param length read length * 读取数据长度 * * @return Read data bytes, or zero length byte array if an error occurs * 读取的字节数组,如果读取过程中出现异常则返回长度为0的字节数组 */ public static byte[] readBytes(final InputStream inputStream, final int offset, final int length) { ByteArrayOutputStream byteArrayOutputStream = null; byte[] content; try { byteArrayOutputStream = new ByteArrayOutputStream(Globals.DEFAULT_BUFFER_SIZE); byte[] readBuffer = new byte[Globals.DEFAULT_BUFFER_SIZE]; int position = Globals.INITIALIZE_INT_VALUE, readLength = Globals.INITIALIZE_INT_VALUE, currentLength; while ((currentLength = inputStream.read(readBuffer)) != Globals.DEFAULT_VALUE_INT) { if (offset != Globals.DEFAULT_VALUE_INT && (position + currentLength) < offset) { position += currentLength; continue; } int off, len; if (offset == Globals.DEFAULT_VALUE_INT || offset < position) { off = Globals.INITIALIZE_INT_VALUE; } else { off = offset - position; } if (length == Globals.DEFAULT_VALUE_INT || (readLength + currentLength) < length) { len = currentLength; } else { len = length - readLength; if (currentLength < len) { len = currentLength; } } byteArrayOutputStream.write(readBuffer, off, len); position += currentLength; readLength += len; } content = byteArrayOutputStream.toByteArray(); } catch (Exception e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } content = new byte[0]; } finally { closeStream(byteArrayOutputStream); } return content; } /** *

Read data bytes from given input stream instance use default charset: UTF-8

*

使用UTF-8编码从给定的输入流实例对象中读取字节数组

* * @param inputStream input stream instance * 输入流实例对象 * * @return Read string, or zero length string if an error occurs * 读取的字符串,如果读取过程中出现异常则返回空字符串 */ public static String readContent(final InputStream inputStream) { return IOUtils.readContent(inputStream, Globals.DEFAULT_ENCODING); } /** *

Read string from given input stream instance

*

从给定的输入流实例对象中读取字符串

* * @param inputStream input stream instance * 输入流实例对象 * @param encoding Charset encoding * 字符集编码 * * @return Read string, or zero length string if an error occurs * 读取的字符串,如果读取过程中出现异常则返回空字符串 */ public static String readContent(final InputStream inputStream, final String encoding) { char [] readBuffer = new char[Globals.DEFAULT_BUFFER_SIZE]; int len; StringBuilder returnValue = new StringBuilder(); InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { inputStreamReader = new InputStreamReader(inputStream, encoding); bufferedReader = new BufferedReader(inputStreamReader); while ((len = bufferedReader.read(readBuffer)) > -1) { returnValue.append(readBuffer, 0, len); } } catch (Exception e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } return returnValue.toString(); } finally { closeStream(inputStreamReader); closeStream(bufferedReader); closeStream(inputStream); } return returnValue.toString(); } /** *

Copy data bytes from given input stream instance to output stream instance

*

从给定的输入流实例对象中复制数据到给定的输出流实例对象中

* * @param inputStream input stream instance * 输入流实例对象 * @param outputStream output stream instance * 输出流实例对象 * @param closeOutputAfterCopy close output stream after copy * 是否在完成复制后关闭输出流实例对象 * * @return Copy length of data bytes * 复制的数据长度 * * @throws IOException * if an I/O error occurs * 当复制过程中出现异常 */ public static long copyStream(final InputStream inputStream, final OutputStream outputStream, final boolean closeOutputAfterCopy) throws IOException { return copyStream(inputStream, outputStream, closeOutputAfterCopy, new byte[Globals.DEFAULT_BUFFER_SIZE]); } /** *

Copy data bytes from given input stream instance to output stream instance using given buffer

*

使用给定的缓冲区从给定的输入流实例对象中复制数据到给定的输出流实例对象中

* * @param inputStream input stream instance * 输入流实例对象 * @param outputStream output stream instance * 输出流实例对象 * @param closeOutputAfterCopy close output stream after copy * 是否在完成复制后关闭输出流实例对象 * @param buffer copy buffer * 复制缓冲区 * * @return Copy length of data bytes * 复制的数据长度 * * @throws IOException * if an I/O error occurs * 当复制过程中出现异常 */ public static long copyStream(final InputStream inputStream, final OutputStream outputStream, final boolean closeOutputAfterCopy, final byte[] buffer) throws IOException { if (inputStream == null) { return 0L; } try { long totalCount = 0L; int readCount = inputStream.read(buffer); while (readCount != Globals.DEFAULT_VALUE_INT) { totalCount += readCount; outputStream.write(buffer, 0, readCount); readCount = inputStream.read(buffer); } outputStream.flush(); return totalCount; } finally { closeStream(inputStream); if (closeOutputAfterCopy) { closeStream(outputStream); } } } /** *

Close current stream instance

*

关闭给定的流实例对象

* * @param closeable Stream instance will close * 即将关闭的流实例对象 */ public static void closeStream(final Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { LOGGER.error("Close_Stream_IO_Error"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy