java 读取输入流

java 中如何读取输入流呢?

方式一:

/***
	 * Has been tested ok
	 * 
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBytes3(InputStream in) throws IOException {
		BufferedInputStream bufin = new BufferedInputStream(in);
		int buffSize = 1024;
		ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);

		// System.out.println("Available bytes:" + in.available());

		byte[] temp = new byte[buffSize];
		int size = 0;
		while ((size = bufin.read(temp)) != -1) {
			out.write(temp, 0, size);
		}
		bufin.close();
		in.close();
		byte[] content = out.toByteArray();
		out.close();
		return content;
	}

 

方式二:

/***
	 * get byte[] from <code>InputStream</code> Low efficiency
	 * 
	 * @param in
	 * @return
	 * @throws IOException
	 */
	@Deprecated
	public static byte[] readBytes2(InputStream in) throws IOException {
		byte[] temp = new byte[1024];
		byte[] result = new byte[0];
		int size = 0;
		while ((size = in.read(temp)) != -1) {
			byte[] readBytes = new byte[size];
			System.arraycopy(temp, 0, readBytes, 0, size);
			result = mergeArray(result, readBytes);
		}
		return result;
	}
/***
	 * 合并字节数组
	 * 
	 * @param a
	 * @return
	 */
	public static byte[] mergeArray(byte[]... a) {
		// 合并完之后数组的总长度
		int index = 0;
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum = sum + a[i].length;
		}
		byte[] result = new byte[sum];
		for (int i = 0; i < a.length; i++) {
			int lengthOne = a[i].length;
			if (lengthOne == 0) {
				continue;
			}
			// 拷贝数组
			System.arraycopy(a[i], 0, result, index, lengthOne);
			index = index + lengthOne;
		}
		return result;
	}

 

方式三:

/***
	 * 指定字符编码,无损地读取文本文件.推荐!
	 * 
	 * @param in
	 *            : 输入流,会关闭
	 * @param charset
	 *            : 字符编码
	 * @return
	 * @throws IOException
	 */
	public static StringBuffer getFullContent3(InputStream in, String charset)
			throws IOException {
		StringBuffer sbuffer = new StringBuffer();
		InputStreamReader inReader;
		// 设置字符编码
		if (ValueWidget.isNullOrEmpty(charset)) {
			charset = SystemHWUtil.CURR_ENCODING;
		}
		inReader = new InputStreamReader(in, charset);
		char[] ch = new char[1024];
		int readCount = 0;
		while ((readCount = inReader.read(ch)) != -1) {
			sbuffer.append(ch, 0, readCount);
		}
		inReader.close();
		in.close();
		return sbuffer;
	}

 

方式四:

/***
	 * 先读取出来字节数组,然后在包装成为字符串;效率不高,因为有拷贝操作(System.arraycopy)
	 * 
	 * @param in
	 * @param charset
	 * @return
	 * @throws IOException
	 */
	public static String getFullContent2(InputStream in, String charset)
			throws IOException {
		//并不是要读取的字节的长度
		int step = BUFFSIZE_1024;
		BufferedInputStream bis = new BufferedInputStream(in);

		// Data's byte array
		byte[] receData = new byte[step];

		// data length read from the stream
		int readLength = 0;

		// data Array offset
		int offset = 0;

		// Data array length
		int byteLength = step;

		while ((readLength = bis.read(receData, offset, byteLength - offset)) != -1) {
			// Calculate the current length of the data
			offset += readLength;
			// Determine whether you need to copy data , when the remaining
			// space is less than step / 2, copy the data
			if (byteLength - offset <= step / 2) {
				byte[] tempData = new byte[receData.length + step];
				System.arraycopy(receData, 0, tempData, 0, offset);
				receData = tempData;
				byteLength = receData.length;
			}
		}

		return new String(receData, 0, offset, charset);
	}

 

方式五:

/***
	 * write inputstream into outputStream ,haven't close stream.
	 * 
	 * @param ins
	 * @param outs
	 */
	public static void writeIn2Output(InputStream ins, OutputStream outs,
			boolean isCloseOut, boolean isCloseInput) {
		try {
			int resultInt = -1;
			byte[] bytes = null;
			bytes = new byte[4096];

			try {
				while ((resultInt = ins.read(bytes)) != -1) {
					outs.write(bytes, 0, resultInt);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (isCloseOut) {
					try {
						outs.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (isCloseInput) {
					try {
						ins.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} finally {

		}
	}

 

方式六:

/***
	 * write inputstream into file according to specified length.
	 * 
	 * @param file
	 * @param ins
	 * @param length2
	 * @throws IOException
	 */
	public static void writeInputStream2File(File file, InputStream ins,
			long length2, boolean isShutOutputStream) throws IOException {
		String parentDir = SystemHWUtil.getParentDir(file.getAbsolutePath());
		File fatherFile = new File(parentDir);
		if (!fatherFile.exists()) {
			fatherFile.mkdirs();
		}
		FileOutputStream outs = new FileOutputStream(file);
		int readSize;
		byte[] bytes = null;
		bytes = new byte[(int) length2];

		long length_tmp = length2;
		while ((readSize = ins.read(bytes)) != -1) {
			length_tmp -= readSize;

			outs.write(bytes, 0, readSize);
			if (length_tmp == 0) {
				break;
			}
			if (length_tmp < 4096) {
				bytes = new byte[(int) length_tmp];
			}
		}
		if (isShutOutputStream) {
			outs.close();
		}

	}

 

方式七:

/***
	 * 从输入流获取字节数组
	 * @param br_right
	 * @param length2
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBytesFromInputStream(BufferedInputStream br_right,
			int length2) throws IOException {
		int readSize;
		byte[] bytes = null;
		bytes = new byte[length2];

		long length_tmp = length2;
		long index =0;//start from zero
		while ((readSize = br_right.read(bytes,(int)index,(int)length_tmp)) != -1) {
			length_tmp -= readSize;
			if (length_tmp == 0) {
				break;
			}
			index=index+readSize;
		}
		return bytes;
	}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值