[转帖]异步IO中读写Object
2006-08-10 14:07
239 查看
int INT_LENGTH = 4;
/**
*
* <p>[机 能] 发送Object </p>
*
* @param obj 要发送的Object
* @return 发送字节数
* @throws IOException
*/
public int writeObject(Object obj) throws IOException {
ByteBuffer byteBuf = null;
byteBuf = ByteBuffer.wrap(serializer(obj));
return writeObject(byteBuf);
}
/**
*
* <p>[机 能] 发送Object </p>
*
* @param byteBuf ByteBuffer类型Object
* @return 发送字节数
* @throws IOException
*/
public int writeObject(ByteBuffer byteBuf) throws IOException {
byteBuf.rewind();
int len = byteBuf.limit();
ByteBuffer outBuf = ByteBuffer.allocate(INT_LENGTH + len);
outBuf.putInt(len);
outBuf.put(byteBuf);
outBuf.flip();
//发送内容为: Object长度 + Object自身
return write(outBuf);
}
/**
*
* <p>[机 能] 接收Object </p>
*
* @return 读取的Object
* @throws IOException
*/
public Object readObject() throws IOException {
Object readObj = null;
int bytes = -1;
int bytesTotal = 0;
ByteBuffer bufLen = null;
ByteBuffer bufTemp = null;
ByteBuffer bufMsg = null;
/* 读取Object的长度 */
bufLen = ByteBuffer.allocateDirect(INT_LENGTH);
bufTemp = ByteBuffer.allocateDirect(INT_LENGTH);
bytes = -1;
bytesTotal = 0;
/* 读取INT_LENGTH个字节作为Object的长度 */
while (0 < (bytes = read(bufTemp))) {
bytesTotal += bytes;
bufTemp.flip();
bufLen.put(bufTemp);
if (bytesTotal < INT_LENGTH) {
// 长度读取未完
bufTemp = ByteBuffer.allocateDirect(INT_LENGTH - bytesTotal);
} else {
break;
}
}
if (0 == bytesTotal) {
return null;
}
if (bytesTotal < INT_LENGTH) {
// 读取到的字节数少于约定的字节数,作为通信ERROR处理
throw new IOException("Object长度读取出错。目前读取字节数:"
+ bytesTotal);
}
int len = 0;
try {
bufLen.flip();
len = bufLen.getInt();
} catch (Exception e) {
throw new IOException("读取到的字节非Object长度");
}
if (0 == len) {
return null;
}
/* 读取Object自身 */
bufTemp = ByteBuffer.allocate(len);
bufMsg = ByteBuffer.allocate(len);
bytes = -1;
bytesTotal = 0;
/* 读取len(前面取到的长度)个字节作为Object自身 */
while (0 < (bytes = read(bufTemp))) {
bytesTotal += bytes;
bufTemp.flip();
bufMsg.put(bufTemp);
if (bytesTotal < len) {
// Object自身读取未完了
bufTemp = ByteBuffer.allocateDirect(len - bytesTotal);
} else {
break;
}
}
if (bytesTotal < len) {
// 读取的字节数少于约定的字节数,作为通信ERROR处理
throw new IOException("Object读取出错");
}
try {
bufMsg.flip();
byte[] buf = new byte[bufMsg.limit()];
bufMsg.get(buf);
readObj = deSerializer(buf);
} catch (IOException ioe) {
// Object转换出错
throw ioe;
} catch (ClassNotFoundException cnfe) {
throw new IOException("ByteBuffer到Object转换出错(ClassNotFoundException)");
}
return readObj;
}
/**
* <p>[机 能] 将Object转换成byte数组 </p>
*
* @param obj 需要转换的Object
* @return 转换后的byte数组
* @throws IOException
*/
public static byte[] serializer(Object obj) throws IOException {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
return baos.toByteArray();
} catch (IOException e) {
throw e;
} finally {
if (oos != null) {
oos.close();
}
}
}
/**
* <p>[机 能] 将byte数组转换成Object </p>
*
* @param org 需要转换的byte数组
* @return 转换后的Object
* @throws IOException
* @throws ClassNotFoundException
*/
public Object deSerializer(byte[] org) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(org));
return ois.readObject();
}
出处:http://community.csdn.net/Expert/topic/4890/4890278.xml?temp=.712948
/**
*
* <p>[机 能] 发送Object </p>
*
* @param obj 要发送的Object
* @return 发送字节数
* @throws IOException
*/
public int writeObject(Object obj) throws IOException {
ByteBuffer byteBuf = null;
byteBuf = ByteBuffer.wrap(serializer(obj));
return writeObject(byteBuf);
}
/**
*
* <p>[机 能] 发送Object </p>
*
* @param byteBuf ByteBuffer类型Object
* @return 发送字节数
* @throws IOException
*/
public int writeObject(ByteBuffer byteBuf) throws IOException {
byteBuf.rewind();
int len = byteBuf.limit();
ByteBuffer outBuf = ByteBuffer.allocate(INT_LENGTH + len);
outBuf.putInt(len);
outBuf.put(byteBuf);
outBuf.flip();
//发送内容为: Object长度 + Object自身
return write(outBuf);
}
/**
*
* <p>[机 能] 接收Object </p>
*
* @return 读取的Object
* @throws IOException
*/
public Object readObject() throws IOException {
Object readObj = null;
int bytes = -1;
int bytesTotal = 0;
ByteBuffer bufLen = null;
ByteBuffer bufTemp = null;
ByteBuffer bufMsg = null;
/* 读取Object的长度 */
bufLen = ByteBuffer.allocateDirect(INT_LENGTH);
bufTemp = ByteBuffer.allocateDirect(INT_LENGTH);
bytes = -1;
bytesTotal = 0;
/* 读取INT_LENGTH个字节作为Object的长度 */
while (0 < (bytes = read(bufTemp))) {
bytesTotal += bytes;
bufTemp.flip();
bufLen.put(bufTemp);
if (bytesTotal < INT_LENGTH) {
// 长度读取未完
bufTemp = ByteBuffer.allocateDirect(INT_LENGTH - bytesTotal);
} else {
break;
}
}
if (0 == bytesTotal) {
return null;
}
if (bytesTotal < INT_LENGTH) {
// 读取到的字节数少于约定的字节数,作为通信ERROR处理
throw new IOException("Object长度读取出错。目前读取字节数:"
+ bytesTotal);
}
int len = 0;
try {
bufLen.flip();
len = bufLen.getInt();
} catch (Exception e) {
throw new IOException("读取到的字节非Object长度");
}
if (0 == len) {
return null;
}
/* 读取Object自身 */
bufTemp = ByteBuffer.allocate(len);
bufMsg = ByteBuffer.allocate(len);
bytes = -1;
bytesTotal = 0;
/* 读取len(前面取到的长度)个字节作为Object自身 */
while (0 < (bytes = read(bufTemp))) {
bytesTotal += bytes;
bufTemp.flip();
bufMsg.put(bufTemp);
if (bytesTotal < len) {
// Object自身读取未完了
bufTemp = ByteBuffer.allocateDirect(len - bytesTotal);
} else {
break;
}
}
if (bytesTotal < len) {
// 读取的字节数少于约定的字节数,作为通信ERROR处理
throw new IOException("Object读取出错");
}
try {
bufMsg.flip();
byte[] buf = new byte[bufMsg.limit()];
bufMsg.get(buf);
readObj = deSerializer(buf);
} catch (IOException ioe) {
// Object转换出错
throw ioe;
} catch (ClassNotFoundException cnfe) {
throw new IOException("ByteBuffer到Object转换出错(ClassNotFoundException)");
}
return readObj;
}
/**
* <p>[机 能] 将Object转换成byte数组 </p>
*
* @param obj 需要转换的Object
* @return 转换后的byte数组
* @throws IOException
*/
public static byte[] serializer(Object obj) throws IOException {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
return baos.toByteArray();
} catch (IOException e) {
throw e;
} finally {
if (oos != null) {
oos.close();
}
}
}
/**
* <p>[机 能] 将byte数组转换成Object </p>
*
* @param org 需要转换的byte数组
* @return 转换后的Object
* @throws IOException
* @throws ClassNotFoundException
*/
public Object deSerializer(byte[] org) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(org));
return ois.readObject();
}
出处:http://community.csdn.net/Expert/topic/4890/4890278.xml?temp=.712948
相关文章推荐
- Java IO系列(五):读写对象ObjectOutputStream和ObjectInputStream
- Python笔记-IO同步和异步、 读写文件、StringIO和BytesIO
- windows io 异步读写
- IO与文件读写---同步/异步与阻塞/非阻塞的区别(转) _适用于TI dsp驱动
- IO三种方式:BIO,NIO,AIO (异步读写asynchronous IO)
- IO三种方式:BIO,NIO,AIO (异步读写asynchronous IO)
- IO - 同步,异步,阻塞,非阻塞 (亡羊补牢篇)
- IO中同步、异步与阻塞、非阻塞的区别
- 查看linux服务器硬盘IO读写负载
- 异步IO
- IO - 同步,异步,阻塞,非阻塞
- 深入剖析Nodejs的异步IO
- 异步读写文件
- 网络IO之阻塞、非阻塞、同步、异步总结
- 关于IO的同步、异步、阻塞、非阻塞
- 查看linux服务器硬盘IO读写负载
- but the supplied types were (flex.messaging.io.amf.ASObject) and converted to (null)."
- 【Python】[IO编程]文件读写,StringIO和BytesIO,操作文件和目录,序列化
- 为什么 Node.js 的异步 IO 具有非常好的性能
- 异步读写数据流