我有一个原始字节数组,我需要将其标记为 java 中的字节数组列表。通过以下方法声明更好地解释。
public static List<byte[]> splitMessage(byte[] rawByte, String tokenDelimiter)
示例运行。
示例运行 1:
原始字节
byte[] rawBytes = new byte[]{72,118,121,49,85,118,97,113,111,124,44,124,49,48,43,57,48,36,63,49,66,70,22,18,124,44,124,23,27,25,54,24,24,34,44,57,69,66,49,47,66,16,39,35,32,36,30,50,63,124,44,124,16,18,24,64,4,94,124,44,124,19,31,42,55,66,46,34,62,34,37};
tokenDelimiter
是|,|
(即124,44,124
)
所以返回的列表如下:
Token 1: 72,118,121,49,85,118,97,113,111
Token 2: 49,48,43,57,48,36,63,49,66,70,22,18
Token 3: 23,27,25,54,24,24,34,44,57,69,66,49,47,66,16,39,35,32,36,30,50,63,
Token 4: 16,18,24,64,4,94
Token 5: 19,31,42,55,66,46,34,62,34,37
示例运行 2:
byte[] rawBytes = new byte[]{72,118,121,49,85,118,97,113,111,124,44,124,49,48,43,57,48,36,63,49,66,70,22,18,124,44,124,124,44,124,23,27,25,54,24,24,34,44,57,69,66,49,47,66,16,39,35,32,36,30,50,63,124,44,124,16,18,24,64,4,94,124,44,124,19,31,42,55,66,46,34,62,34,37,124,44,124,124,44,124};
tokenDelimiter
是|,|
(即124,44,124
)
Token 1: 72,118,121,49,85,118,97,113,111
Token 2: 49,48,43,57,48,36,63,49,66,70,22,18
Token 3: <Empty>
Token 3: 23,27,25,54,24,24,34,44,57,69,66,49,47,66,16,39,35,32,36,30,50,63,
Token 4: 16,18,24,64,4,94
Token 5: 19,31,42,55,66,46,34,62,34,37
Token 6: <Empty>
Token 7: <Empty>
我能够从以下代码片段中运行示例。但在第二个中卡住了标签。
public static List<byte[]> splitMessageSept19(byte[] rawByte, String tokenDelimiter) throws UnsupportedEncodingException
{
List<byte[]> tokens = new ArrayList<byte[]>();
final byte[] byteArray = tokenDelimiter.getBytes("UTF-8");
final byte byteDelimitorFirstByte = byteArray[0];
int bytenum =0 ;
int lastIndex = 0;
int storIterator =0;
for ( int iterator = 0 ; iterator <= rawByte.length ; iterator++ )
{
if (iterator == rawByte.length || rawByte[iterator] == byteDelimitorFirstByte)
{
storIterator = iterator;
if ( iterator != rawByte.length )
{
for ( int i=0 ; i < byteArray.length ; i++ )
{
if ( rawByte[iterator] == byteArray[i] )
{
iterator++ ;
continue;
}
else
{
break;
}
}
}
byte[] byteArrayExtracted = new byte[storIterator - lastIndex];
System.arraycopy(rawByte, lastIndex, byteArrayExtracted, 0,
storIterator - lastIndex);
lastIndex = iterator ;
tokens.add(byteArrayExtracted);
byteArrayExtracted = null;
}
}
for ( byte[] bytetoken : tokens )
{
System.out.println("Token received is: " + new String(bytetoken, "UTF-8"));
}
return tokens;
}
有没有人遇到过类似的标记数组的问题?请建议是否有其他方法来标记数组。
请注意:我不想将字节流转换为String
,以字符串格式标记化并转换回字节。它可能有编码问题。