How did I read a BufferedFile using read (ubyte [] buffer) when the buffer length is set at runtime?

I have a binary file that really is a stack of files, format:

lengh_of_subfile,subfile 

length_of_subfile is a 64-bit integer. I can read long without any problems, but when I try to create a buffer for a subfile, I get compilation errors saying that it cannot be read at compile time. What am I missing? I wrote an identical extraction tool in erlang, PHP and C # ... D throws me in 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++; } } 
+6
source share
3 answers

Instead

  ubyte[len] ogg; 

records

  ubyte[] ogg = new ubyte[len]; 
+7
source

edit what you want to fill

 ubyte[1024*8] ogg; ogg=ogg[0..len] file.read(ogg); 

or use a loop to copy (as an array of size 2 ^ 64 bytes will not fit into memory)

 ubyte[1024*16] ogg; while(len>0 && (int read=file.read(ogg[0..$>len?len:$]))!=0){ oggout.write(ogg[0..read]); len-=read;//len is the amount still to be read } 

side note writeln("Creating file ",counter, ".ogg"); more efficient than concat and then writes (java path) because it does not create useless lines (and creating a format string at runtime asks for an error sooner or later than the first % you do not take into account)

+2
source

You can use an array with dynamic length or just use the new one to create a new ubyte array:

 new ubyte[len] 
+1
source

Source: https://habr.com/ru/post/904819/


All Articles