我有一個原始字節數組,我需要將其標記爲java中的一個字節數組列表。以下方法聲明更好地解釋了這一點。Tokenize字節數組
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
,以字符串格式進行標記並轉換回字節。它可能存在編碼問題。
爲什麼你不只是跳過你的代碼中的空令牌? – mishadoff