1.序列化和反序列化:
- 序列化是對象(類的實例)轉換成字節數組或者字符串通過網絡傳輸或者存儲到本地文件。反序列化:就是將字節數組或字符串在轉換成對象實例的過程。
- (因為在網絡中傳輸或者寫本地文件,是不能使用對象的,tcp握手之后會建立一個字節流管道傳輸數據,所以要將對象轉換序列化成字節序列)
2.ByteArrayOutputStream、ByteArrayInputStream:
- 這兩個流實際就內存流:顧名思義就是將數據寫入內存,從內存中讀取數據;
- ByteArrayOutputStream:字節數組輸出流在內存中創建一個字節數組緩沖區,所有發送到輸出流的數 據保存在該字節數組緩沖區中。實際作用就是通過write()將對象各個字段寫入一個字節數組,然后在使用toByteArray()將字節數據取出來,通過tcp傳輸給服務器。
- ByteArrayInputStream:字節數組輸入流在內存中創建一個字節數組緩沖區,從輸入流讀取的數據保存在該字節數組緩沖區中。實際就是將客戶端發送過來的消息轉成byte數組,存入內存,在分批次讀取數據。
代碼如下:
- 實例類:
我這里實體類數據類型是這樣的:類型和子類型都是1字節,長度是4字節就是一個int,實際數據包含兩部分UUID+數據
(代碼免費下載鏈接在最后)
public class Message implements Serializable {public int type
; public int subtype
; public int dataLength
; public String uniqueIdentifies
; public String details
= ""; public int getType() {return type
;}public void setType(int type
) {this.type
= type
;}public int getSubtype() {return subtype
;}public void setSubtype(int subtype
) {this.subtype
= subtype
;}public int getDataLength() {return dataLength
;}public void setDataLength(int dataLength
) {this.dataLength
= dataLength
;}public String
getUniqueIdentifies() {return uniqueIdentifies
;}public void setUniqueIdentifies(String uniqueIdentifies
) {this.uniqueIdentifies
= uniqueIdentifies
;}public String
getDetails() {return details
;}public void setDetails(String details
) {this.details
= details
;}@Overridepublic String
toString() {return "Message{" +"type=" + type
+", subtype=" + subtype
+", dataLength=" + dataLength
+", uniqueIdentifies='" + uniqueIdentifies
+ '\'' +", details='" + details
+ '\'' +'}';}
}
public class TcpClient {private String host
= "localhost";private int port
= 8189;public TcpClient() {}public TcpClient(String host
, int port
) {this.host
= host
;this.port
= port
;}public void chat(){try {Socket socket
= new Socket(host
,port
);ByteArrayOutputStream bArray
= new ByteArrayOutputStream();try {DataOutputStream out
= new DataOutputStream(socket
.getOutputStream());Message message
= setMessage();System
.out
.println(message
.details
);bArray
.write(message
.getType());bArray
.write(message
.getSubtype());byte[] bytes1
= intToByte(message
.getDataLength());bArray
.write(bytes1
,0,bytes1
.length
);bArray
.write(message
.uniqueIdentifies
.getBytes(), 0, message
.uniqueIdentifies
.length());byte[] bytes2
= message
.details
.getBytes();bArray
.write(bytes2
, 0, bytes2
.length
);bArray
.flush();byte[] bytes
= bArray
.toByteArray();String result
= new String(bytes
);out
.writeUTF(result
);}finally {bArray
.close();socket
.close();}} catch (IOException e
) {e
.printStackTrace();}}public static void main(String
[] args
) {new TcpClient().chat();}public Message
setMessage(){Message message
= new Message();message
.setType(0x01);message
.setSubtype(0x01);message
.setDetails("春?(^_-)");message
.setDataLength(16+message
.getDetails().length());message
.setUniqueIdentifies(new String("WHDISakcmqSiamSq"));return message
;}public byte[] intToByte(int res
) {byte[] targets
= new byte[4];targets
[0] = (byte) (res
& 0xff);targets
[1] = (byte) ((res
>> 8) & 0xff);targets
[2] = (byte) ((res
>> 16) & 0xff);targets
[3] = (byte) (res
>>> 24);return targets
;}
}
package org2
.server
;import org2
.message
.Message
;import java
.io
.ByteArrayInputStream
;
import java
.io
.DataInputStream
;
import java
.io
.IOException
;
import java
.net
.ServerSocket
;
import java
.net
.Socket
;
public class TcpServer {private int port
= 8189;public TcpServer() {}public TcpServer(int port
) {this.port
= port
;}public void service() {try {ServerSocket serverSocket
= new ServerSocket(port
);Socket socket
= serverSocket
.accept();ByteArrayInputStream bInArray
= null
;try {DataInputStream in
= new DataInputStream(socket
.getInputStream());String s
= in
.readUTF();byte[] bytes
= s
.getBytes();int realLength
= bytes
.length
;System
.out
.println("realLength:" + realLength
);bInArray
= new ByteArrayInputStream(bytes
);Message message
= new Message();message
.setType(bInArray
.read());message
.setSubtype(bInArray
.read());byte[] bytesDataLength
= new byte[4];bInArray
.read(bytesDataLength
, 0, 4);int i
= byteToInt(bytesDataLength
);message
.setDataLength(i
);byte[] byteUniqueIdentifies
= new byte[16];bInArray
.read(byteUniqueIdentifies
, 0, 16);message
.setUniqueIdentifies(new String(byteUniqueIdentifies
));byte[] byteDetails
= new byte[realLength
- 22];bInArray
.read(byteDetails
, 0, realLength
- 22);message
.setDetails(new String(byteDetails
));System
.out
.println(message
);} finally {socket
.close();serverSocket
.close();}} catch (IOException e
) {e
.printStackTrace();}}public int byteToInt(byte[] arr
) {int i0
= (int) ((arr
[0] & 0xff) << 0 * 8);int i1
= (int) ((arr
[1] & 0xff) << 1 * 8);int i2
= (int) ((arr
[2] & 0xff) << 2 * 8);int i3
= (int) ((arr
[3] & 0xff) << 3 * 8);return i0
+ i1
+ i2
+ i3
;}public static void main(String
[] args
) {new TcpServer().service();}
}
TcpClient.java 下載
Message.java 下載
TcpServer.java 下載
JAVA的ObjectOutputStream不太適用把應該!
總結
以上是生活随笔為你收集整理的JAVA使用ByteArrayOutputStream、ByteArrayInputStream将对象序列化反序列化,通过JAVA socket实现对象在网络中传输的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。