从 .txt 中一次获取 16 个字节
Getting 16 bytes at a time from .txt
我有两个文件,一个发送,一个接收。我知道两者之间的联系。
可以发送文本:
// Prepare a message
messageToSend = "text that is sent";
lengthMessageToSend = messageToSend.length();
sendingBuffer = messageToSend.getBytes();
// Send the message
myLink.sendFrame(sendingBuffer, lengthMessageToSend);
System.out.println(lengthMessageToSend);
并接收:
// Display the message
messageReceived = new String(receivingBuffer, 0, lengthMessageReceived);
System.out.println("Message received is: [" + messageReceived + "]");
// Prepare a message
messageToSend = "1";
lengthMessageToSend = messageToSend.length();
sendingBuffer = messageToSend.getBytes();
// Send the message
myLink.sendFrame(sendingBuffer, lengthMessageToSend);
我现在尝试发送的文本来自 .txt,但只是在有效载荷内一次发送 16 个字节:
[序列 |伦 |有效载荷 | CHECKSUM ] --> 总共 Header 19 个字节。
最好的方法是什么?
一次只读取 16 个字节(8 个字符)??如果是怎么办?
循环并每次将i
递增16,这个i
表示messageToSend.getBytes()
偏移量的开始-然后将接下来的16个字节(从i
到i+16
,处理剩余少于 16 个字节的情况) 放入 'packet' 并发送。
也使用 getBytes()
的显式编码,否则环境设置可能会导致 'unexpected behavioral changes'。
这样的基本实现可能类似于:
byte[] data = messageToSend.getBytes("UTF-8"); // Specify encoding!
// "For all data, stepping by 16 bytes at a time.."
for (int i = 0; i < data.length; i += 16) {
// How many bytes actually remain (or 16 if more than 16 remain)
var frameSize = Math.min(16, data.length - i);
// Construct frame data from data in i..i+frameSize and send.
// If you want to ALWAYS send 16 bytes, update as appropriate.
byte[] frameData = new byte[frameSize];
System.arraycopy(data, i, frameData, 0, frameSize);
sendFrame(frameData, frameSize);
}
必须修改代码以添加序列号,但这应该不难,因为它总是递增一个。
读取过程类似:根据 length/framing 数据消耗数据包头并处理剩余缓冲区(可能包括下一个数据包的开头)。
// Assuming new frames are always copied-to-start and that there
// if a complete frame ready (can be determined from frame size with)
// two reads. If not, keep reading..
byte[] receivingBuffer = ..;
int offset = 0;
int seq = receivingBuffer[offset++];
int frameSize = receivingBuffer[offset++];
// Again, use an encoding
String content = new String(receivingBuffer, offset, frameSize, "UTF-8");
offset += frameSize;
int checksum = receivingBuffer[offset++];
// Use offset to move extra data in receivingBuffer to start
// of buffer to make it easy to reset offset to 0 and repeat above.
我有两个文件,一个发送,一个接收。我知道两者之间的联系。
可以发送文本:
// Prepare a message
messageToSend = "text that is sent";
lengthMessageToSend = messageToSend.length();
sendingBuffer = messageToSend.getBytes();
// Send the message
myLink.sendFrame(sendingBuffer, lengthMessageToSend);
System.out.println(lengthMessageToSend);
并接收:
// Display the message
messageReceived = new String(receivingBuffer, 0, lengthMessageReceived);
System.out.println("Message received is: [" + messageReceived + "]");
// Prepare a message
messageToSend = "1";
lengthMessageToSend = messageToSend.length();
sendingBuffer = messageToSend.getBytes();
// Send the message
myLink.sendFrame(sendingBuffer, lengthMessageToSend);
我现在尝试发送的文本来自 .txt,但只是在有效载荷内一次发送 16 个字节:
[序列 |伦 |有效载荷 | CHECKSUM ] --> 总共 Header 19 个字节。
最好的方法是什么?
一次只读取 16 个字节(8 个字符)??如果是怎么办?
循环并每次将i
递增16,这个i
表示messageToSend.getBytes()
偏移量的开始-然后将接下来的16个字节(从i
到i+16
,处理剩余少于 16 个字节的情况) 放入 'packet' 并发送。
也使用 getBytes()
的显式编码,否则环境设置可能会导致 'unexpected behavioral changes'。
这样的基本实现可能类似于:
byte[] data = messageToSend.getBytes("UTF-8"); // Specify encoding!
// "For all data, stepping by 16 bytes at a time.."
for (int i = 0; i < data.length; i += 16) {
// How many bytes actually remain (or 16 if more than 16 remain)
var frameSize = Math.min(16, data.length - i);
// Construct frame data from data in i..i+frameSize and send.
// If you want to ALWAYS send 16 bytes, update as appropriate.
byte[] frameData = new byte[frameSize];
System.arraycopy(data, i, frameData, 0, frameSize);
sendFrame(frameData, frameSize);
}
必须修改代码以添加序列号,但这应该不难,因为它总是递增一个。
读取过程类似:根据 length/framing 数据消耗数据包头并处理剩余缓冲区(可能包括下一个数据包的开头)。
// Assuming new frames are always copied-to-start and that there
// if a complete frame ready (can be determined from frame size with)
// two reads. If not, keep reading..
byte[] receivingBuffer = ..;
int offset = 0;
int seq = receivingBuffer[offset++];
int frameSize = receivingBuffer[offset++];
// Again, use an encoding
String content = new String(receivingBuffer, offset, frameSize, "UTF-8");
offset += frameSize;
int checksum = receivingBuffer[offset++];
// Use offset to move extra data in receivingBuffer to start
// of buffer to make it easy to reset offset to 0 and repeat above.