`
java_mzd
  • 浏览: 580649 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

内存流

阅读更多

内存流在写通信的时候用过蛮多次了

可是每次看到李新华书上给内存流的定义

ByteArrayInputStream主要完成将内容写入到内存中

ByteArrayOutputStream主要内存中数据输入

就觉得蛋疼无比

本来明白的也看糊涂了

哥,输出流到底是从内存中输出到哪去啊?输入流写入内存又到底是写在哪里啊?

本来自己都用过很多次了

看到他这个定义又混乱了,简直是蛋疼无比。

 

遂,决定总结一番

 

一。ByteArrayOutputStream 主要是将内存中的数据(还是觉得这个说法很蛋疼),换个说法主要是将我们自己设置的,要输出的数据,输出到某一块内存区域,然后我们可以通过 ByteArrayOutputStream 中的toByteArray()方法,得到一个字节数组,这个字节数组中存放的,就是我设置进去的数据

    使用场景有:在远程通信自己设计的通信协议中,要将每一个具体的消息类打包发送,我们可以通过内存输出流,将所有信息输出到某个内存区域,再通过 toByteArray()得到一个字节数组,那么,我们只要发送这个字节数组就好了

   

/**
	 * 用Format对应格式中ImageIO默认参数把IMAGE打包成BYTE[]
	 * @param image
	 * @return
	 */
	private byte[] compressImage(BufferedImage image, String format) {
		BufferedImage bImage = new BufferedImage(image.getWidth(null), image
				.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		Graphics bg = bImage.getGraphics();
		bg.drawImage(image, 0, 0, null);
		bg.dispose();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			ImageIO.write(bImage, format, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}

 

2.ByteArrayInputStream  内存输入流,所谓的将内容写入内存,其实就是将通过构造方法,传入一个字节数组,然后,我们就可以通过这个输入流一个字节一个字节(或者多个字节)的读取出我们需要的信息

使用场景,当我们接收到一个信息时,放入一个字节数组,然后将该数组放入内存输入流中,我们就可以按字节需要的方式读取这个数组了

   示例代码

	/**
	 * @param data
	 *            从输入流得到的数组
	 * @return 对应消息类对象
	 * @throws Exception
	 */
	public static MessageHead unpackMessage(byte[] data) throws Exception {
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				data);
		DataInputStream dataInputStream = new DataInputStream(
				byteArrayInputStream);
		int totallen = data.length + 4;
		byte type = dataInputStream.readByte();
		System.out.println("tyte----" + type);
		int senderID = dataInputStream.readInt();
		int reciverID = dataInputStream.readInt();
		MessageHead message = new MessageHead();
		message.setTotallen(totallen);
		message.setType(type);
		message.setSenderID(senderID);
		message.setReciverID(reciverID);

		if (type == Constance.MESSAGE_TYPE_REGREQUST) {
			int userID = dataInputStream.readInt();
			String password = readString(dataInputStream, 20);
			RegRequestMessage msg = new RegRequestMessage();
			copyHead(message, msg);
			msg.setUserID(userID);
			msg.setPassWord(password);
			return msg;

		}
		
		return null;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

后话,其实以上理解都很2B

人家都说代码里面是没有秘密的,

我们只要打开这两个类的源代码,

就能轻松发现

/**
 * This class implements an output stream in which the data is 
 * written into a byte array. The buffer automatically grows as data 
 * is written to it. 
 * The data can be retrieved using <code>toByteArray()</code> and
 * <code>toString()</code>.
 * <p>
 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
 * this class can be called after the stream has been closed without
 * generating an <tt>IOException</tt>.
  */
  
/**
 * A <code>ByteArrayInputStream</code> contains
 * an internal buffer that contains bytes that
 * may be read from the stream. An internal
 * counter keeps track of the next byte to
 * be supplied by the <code>read</code> method.
 * <p>
 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
 * this class can be called after the stream has been closed without
 * generating an <tt>IOException</tt>.
 *
 */

 

再稍微看下源码,就什么都明白了。

 

 

反正也写了这面久

就当做是给自己个机会总结总结加深理解吧。

分享到:
评论
2 楼 贾懂凯 2010-11-15  
API级别的程序,我觉得不需要看源码,只要看注释就够了。
1 楼 javafound 2010-11-03  
引用
代码里面是没有秘密

相关推荐

Global site tag (gtag.js) - Google Analytics