我已经加密了视频文件,在解密时我定义了 Bytebyte[] input = new byte[1024];
大小以将其写入输出文件。
在这里,我想在输出文件中写入前 1024 个字节,同时如果想播放该视频文件,我可以播放该输出文件,而无需等待像视频流一样写入整个文件。
当写入第一个 1024 字节时,视频文件将开始播放,直到整个文件被写入。
最佳答案
您必须根据获取数据的位置以及保存/查看数据的位置来设置输入流和输出流。通过对输出进行一些缓冲也可能会提高性能。您应该了解总体思路。
public class DecryptionWotsit {
private final BlockingDeque<Byte> queue = new LinkedBlockingDeque<Byte>();
private final InputStream in;
private final OutputStream out;
public DecryptionWotsit(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}
public void go() {
final Runnable decryptionTask = new Runnable() {
@Override
public void run() {
try {
byte[] encrypted = new byte[1024];
byte[] decrypted = new byte[1024];
while (true) {
int encryptedBytes = in.read(encrypted);
// TODO: decrypt into decrypted, set decryptedBytes
int decryptedBytes = 0;
for (int i = 0; i < decryptedBytes; i++)
queue.addFirst(decrypted[i]);
}
}
catch (Exception e) {
// exception handling left for the reader
throw new RuntimeException(e);
}
}
};
final Runnable playTask = new Runnable() {
@Override
public void run() {
try {
while (true) {
out.write(queue.takeLast());
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
};
Executors.newSingleThreadExecutor().execute(decryptionTask);
Executors.newSingleThreadExecutor().execute(playTask);
}
}
关于java - 如何逐字节写入并连续显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8136224/