当运行时设置缓冲区长度时,如何使用 read(ubyte[] buffer) 读取 BufferedFile?
我有一个二进制文件,实际上是一堆文件,格式为:
lengh_of_subfile,subfile
length_of_subfile
是一个 64 位整数。我可以毫无问题地读取long
,但是当我尝试为子文件创建缓冲区时,我收到编译错误,指出它无法在编译时读取。我缺少什么?我用 erlang、PHP 和 C# 编写了一个相同的提取工具...D 让我陷入了困境。
void main(string args[]) {
Stream file = new BufferedFile(args[1], FileMode.In);
int counter = 0;
while(file.position < file.size) {
ulong len;
file.read(len);
ubyte[len] ogg;
file.read(ogg);
string outname = getcwd() ~ "/" ~ to!string(counter) ~ ".ogg";
Stream oggout = new BufferedFile(outname, FileMode.OutNew);
oggout.write(ogg);
writefln("Creating file " ~ to!string(counter) ~ ".ogg");
counter++;
}
}
I have a binary file that is really a stack of files, the format is:
lengh_of_subfile,subfile
length_of_subfile
is a 64-bit integer. I can read the long
no problem but when I try to create a buffer for the subfile I get compile errors saying it cannot be read at compile time. What am I missing? I've written an identical extraction tool in erlang, PHP and C#... D is throwing me for a loop.
void main(string args[]) {
Stream file = new BufferedFile(args[1], FileMode.In);
int counter = 0;
while(file.position < file.size) {
ulong len;
file.read(len);
ubyte[len] ogg;
file.read(ogg);
string outname = getcwd() ~ "/" ~ to!string(counter) ~ ".ogg";
Stream oggout = new BufferedFile(outname, FileMode.OutNew);
oggout.write(ogg);
writefln("Creating file " ~ to!string(counter) ~ ".ogg");
counter++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而不是
写
Instead of
write
切掉您想要填充的内容
或使用循环进行复制(因为 2^64 字节数组无法容纳在内存中)
旁注
writeln("Creating file ",counter, ".ogg"); 比 concat then write (java 方式)更有效,因为它不会创建无用的字符串(并且在运行时创建格式字符串迟早会在您的第一个
%
上询问错误)不考虑)slice off what you want to fill
or use a loop to do the copying (as a 2^64 byte array wont fit in memory)
side note
writeln("Creating file ",counter, ".ogg");
is more efficient than concat then write (the java way) because it doesn't create useless strings (and creating the format string at runtime is asking for a error sooner or later on the first%
you don't account for)您可以使用具有动态长度的数组或仅使用 new 创建一个新的 ubyte 数组:
You could use a array with dynamic length or just use new to create a new ubyte array: