内存流在写通信的时候用过蛮多次了
可是每次看到李新华书上给内存流的定义
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>.
*
*/
再稍微看下源码,就什么都明白了。
哎
反正也写了这面久
就当做是给自己个机会总结总结加深理解吧。
分享到:
相关推荐
Delphi 内存流数组合并成单一内存流,使用方法:1.多个图片、控件、窗体、无类型数组等转换成一个个内存流,2.利用本单元合并成一个内存流,3.保存成一个文件,4.调用时:由文件生成单一内存流,5.利用本单元分解成...
FileStream MemoryStream 内存流 文件流 生成客户端 FileStream MemoryStream 内存流 文件流 生成客户端
适用于asp.net MVC中导出Excel,程序使用NPOI,并以内存流的方式提交到界面上,点击下载Excel。
delphi用内存流支播放任意视频,自己的视频也可以
C++内存流CMemoryStream,利用内存池提高内存分配效率和速度
利用NPOI导出Excel文件,写入到内存流中,输出文件下载
利用内存流实现高速的大文件读写操作。可避免把开过G的文件时,造成假死现象。
一个详细介绍JAVA_内存流的例子 想可以帮助到你。
很多木马生成器就是用的内存流和文件流生成客户端的,废话不多说了,大家看代码吧!
Delphi内存流读写、删除、输入输出实例,将若干个流合并成一个流,可以对其进行读出添加删除操作以及输出为文件或流.zip
流和内存流进行转换
Visual C++源代码 155 如何使用内存流操作无文件XMLVisual C++源代码 155 如何使用内存流操作无文件XMLVisual C++源代码 155 如何使用内存流操作无文件XMLVisual C++源代码 155 如何使用内存流操作无文件XMLVisual ...
vC6.0使用,自己编程的,比书上有所改进,内存占用小,可以参考一下
图片文件、文本文件转为内存流,及内存流转为相应的文件。
这个问题对没有经验的开发者而言会很容易被难倒,因为要完全开发一个播放器去集成到应用中显得很不现实,而WindowsMediaPlayer又不提供从内存流播放的方法,在网上搜索资料又很少涉及这方面的,其实有一个很简单的...
delphi 利用内存流合并文件 delphi 利用内存流合并文件
Delphi文件流、内存流原理实例及应用,了解一下如何使用文件流和内存流生成一些程序,好像一些外壳程序就是使用的内存流,挺底层的,不是太了解,不便多说。
使用内存流操作无文件XML 编程小实例,C++.net源代码编写
一个内存流的合并添加删除读出输出的类,含DEMO。
一个内存流的合并添加删除读出输出的类(含DEMO)