我必須在我的Android應用程序中填寫一個字節[]。有時候,這個大於4KB。 我初始化我的byte []是這樣的:限制大小字節[] Java android
int size = ReadTools.getPacketSize(ptr.dataInputStream);
byte[] myByteArray = new byte[size];
在這裏,我的大小= 22625.但是當我填寫了我的byte []是這樣的:
while (i != size) {
myByteArray[i] = ptr.dataInputStream.readByte();
i++;
}
但是當我打印的內容我的字節[],我有一個字節[]與大小= 4060. 是否Java分裂我的字節[],如果這一個大於4060?如果是的話,我怎麼能有一個字節[]超過4060?
這裏是我的全碼:
public class ReadSocket extends Thread{
DataInputStream inputStream;
BufferedReader reader;
GlobalContent ptr;
public ReadSocket(DataInputStream inputStream, GlobalContent ptr)
{
this.inputStream = inputStream;
this.ptr = ptr;
}
public void run() {
int i = 0;
int j = 0;
try {
ptr.StatusThreadReadSocket = 1;
while(ptr.dataInputStream.available() == 0)
{
if(ptr.StatusThreadReadSocket == 0)
{
ptr.dataInputStream.close();
break;
}
}
if(ptr.StatusThreadReadSocket == 1)
{
int end = ReadTools.getPacketSize(ptr.dataInputStream);
byte[] buffer = new byte[end];
while (i != end) {
buffer[j] = ptr.dataInputStream.readByte();
i++;
j++;
}
ptr.StatusThreadReadSocket = 0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
...
}
什麼是「結束」變量它包含什麼值? – Ewald
對不起。這不是「結束」,而是「大小」值。 – Vte
您在調試時將「大小」值打印到控制檯,以確保其大小符合預期?正在討論的緩衝區被初始化,如代碼的第一部分所示,對吧?我對ReadTools不熟悉,因此也不瞭解這些問題。 – Ewald