FileStream をラップする DataStream を使用して、2 つの異なるアプリ間で大きなビットマップを送信しています (インテントには 1 MB の制限があり、ファイルをファイル システムに保存したくありません)。
私の問題は、ストリームがまだ開いているがデータがないときにDataInputStreamがスローしていることです。EOFException
私はこれが単にブロックされることを期待していました (ただし、ドキュメントはこの問題について非常に曖昧です)。
データ出力ストリーム:
public void onEvent() {
fos.writeInt(width);
fos.writeInt(height);
fos.writeInt(newBuffer.length);
fos.write(newBuffer);
}
データ入力ストリーム:
while(true) {
int width = fis.readInt();
int height = fis.readInt();
int length = fis.readInt();
byte[] bytes = new byte[length];
fis.read(bytes);
}
あるスレッドから別のスレッドにデータをストリーミングするためのより良いクラスのセットを提案できますか (ここで、read() / readInt() は正常にブロックされます)。
編集
私は単純に andを使って式からDataInputStream
andを取り除くことで、これを解決しようとしました:DataOutputStream
FileInputStream
FileOutputStream
fos.write(intToByteArray(width));
fos.write(intToByteArray(height));
fos.write(intToByteArray(newBuffer.length));
Log.e(this.class.getName(), "Writing width: " + Arrays.toString(intToByteArray(width)) +
", height: " + Arrays.toString(intToByteArray(height)) +
", length: " + Arrays.toString(intToByteArray(newBuffer.length)));
fos.write(newBuffer);
if(repeat == -1) {
Log.e(this.class.getName(), "Closing ramFile");
fos.flush();
fos.close();
}
与える:
Writing width: [0, 0, 2, -48], height: [0, 0, 5, 0], length: [0, 56, 64, 0]
反対側ではこれを使用します:
while(true) {
byte[] intByteArray = new byte[] { -1,-1,-1,-1 };
fis.read(intByteArray);
Log.e(this.class.getName(), Arrays.toString(intByteArray));
int width = toInt(intByteArray, 0);
fis.read(intByteArray);
int height = toInt(intByteArray, 0);
fis.read(intByteArray);
int length = toInt(intByteArray, 0);
Log.e(this.class.getName(), "Reading width: " + width + ", height: " + height + ", length: " + length);
}
これにより、次のことが得られます。
[0, 0, 2, -48]
Reading width: 720, height: 1280, length: 3686400
そして奇妙なことに、read()
ブロックせず、ブロックせずに配列に値を入力せずに陽気に続行します(配列を { 9, 9, 9, 9 } に初期化しても、読んだ)。
[-1, -1, -1, -1]
Reading width: -1, height: -1, length: -1
java.lang.NegativeArraySizeException: -1
狂っているとはこういうことか。