Node.js how to read and write arrays to file as binary - arrays

I have a big array that I need to read in from a file. Normally I would save it as JSON, but the size of the UTF-8 encoded file is substantially larger and I have very strict size requirements so every Kb is precious.
Here's what I have so far:
// https://www.npmjs.com/package/typedarray-to-buffer
var toBuffer = require('typedarray-to-buffer');
var buffer = toBuffer(myArray);
var wstream = fs.createWriteStream('./myArray');
wstream.write(buffer);
// later...
var buff = new Buffer(data); // <-- file data passed in as a buffer
myArray = buff; // <--still binary :-(
I'm following this helpful article about writing binary files but it doesn't cover how to read them back in. I think the part I'm stuck in is turning the buffer data back into an array.
Update
Here's the console.log() for the array before it's written to file:
Int32Array {
'0': 2107281600,
'1': -370226405,
'2': 274543611,
'3': 172775319,
'4': -1927927544,
'5': -248215383,
'6': -1295527238,
'7': -1774538531,
'8': -784581845,
'9': 651425656,
'10': -534521241,
'11': -1788883022,
'12': 1679049410,
'13': -1728518340,
...

It is actually very simple to write binary to file. Use the "binary" encoding type string for the writeFileSync method from FS. Like so :
// Assume you have an Int32Array called myArray
var fs = require('fs');
fs.writeFileSync('/tmp/test.bin', myArray, 'binary');
Now you can check that the size of the file matches the byte size of the typed array. For example, myArray.length*4 will be the size of your data which in my case is 1228624. The file system says :
$ ls -l /tmp/test.bin
-rw-rw-r-- 1 flatmax flatmax 1228624 May 5 17:58 /tmp/test.bin

Related

Adding numbers to each line in a file in haxe language

Need some help, writing a program that will read the text from a file, but will
put at the beginning of each line a number, in such a way that each line is numbered in ascending order
example:
file1
a
b
c
What I want to see:
1: a
2: b
3: c
Process:
Read contents of file into a String
Split by line ending into Array<String>
Iterate and mutate contents line by line
Join by line ending back into a String
Write back into file
Sample code for any sys target:
var arr = sys.File.getContent('file.txt').split("\n");
for(i in 0...arr.length) {
arr[i] = (i+1) + ": " + arr[i];
}
sys.File.saveContent('file.txt', arr.join("\n"));

Remove element from node.js Buffer

I have a node.js buffer declared in this way;
var buffer_bin;
buffer_bin = new Buffer("ABCDEF", "hex");
Contents of buffer_bin is "ab cd ef" in binary bytes.
I want to remove the first byte ab from buffer_bin such that the contents of buffer_bin becomes "cd ef".
Use the slice method in Buffer object.
var new_buffer_bin = buffer_bin.slice(1);

Angularjs switch statement

I am fetching few values from my backend and assigning then to my scope variables in angular controller. for one of the variables I get the values between 1 till 7 and depending on the which number it is I want to do something like following:
.success(function(response){
$scope.initial_data=response;
angular.forEach($scope.initial_data, function(item){
$scope.exercise_id=item.exercise_type;
alert($scope.exercise_id) // This gives me either 1 or any number till 7
switch($scope.exercise_id){
case '1':
alert("1");
break;
case '2':
alert("2");
break;
default:
alert("Default");
}
However, this piece of code for switch statement always alerts gives me Default. What am I doing wrong?

Is there a way to change local variables partway through switch case without exiting the switch statement?

I'm dealing with a rather large enumeration, which can conceptually be divided as representing four different categories. Currently I'm using this enumeration in a switch case statement in order to map values into four separate arrays (in line with the four different categories).
What I'm curious about is if it is possible to change a locally defined variable after arbitrary cases in the switch case statement. This would allow for the ability to break the switch statement into these four different sections, and value assignments that would occur for each case -- if equivalent -- can occur at these sections.
A simplified example what I'm going for is as follows:
Setup
enum incidental_indexes {
arr1_0, arr1_2, arr2_0, arr1_1, arr2_1
} indexes;
struct foobar{
int arr1[3];
int arr2[2];
}
enum indexes unknown_index = ???; // In my code there are two separate indexes being mapped
// from one another, so for the sake of example imagine that
// this index is unknown
enum indexes curr_index = arr1_1; //Value here does not matter
struct foobar my_struc;
int * curr_arr;
int mapped_index;
Brute force approach
switch(unknown_index){
case(arr1_0):
curr_arr = my_struc.arr_1; //First category array
curr_index = arr1_0;
break;
case(arr1_1):
curr_arr = my_struc.arr_1; //First category array, again
curr_index = arr1_1;
break;
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
curr_arr = my_struc.arr_2; //Second category array
break;
case(arr2_1):
curr_index = arr2_1;
curr_arr = my_struc.arr_2; //....
break;
}
Ideal Approach
switch(unknown_index){
default: //Notice no break.
curr_arr = my_struc.arr_1; //First category array
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
default: //Something like a second default, however disallowed
curr_arr = my_struc.arr_2; //Second category array
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}
The functional benefits are obviously nill, however I'm curious if this functionality even exists in C, or if there is perhaps a better method for executing this.
Thanks!
Switch statements only perform a single branch, so you can't jump around inside of the switch like that. What you can do however is group certain cases together without a break in between:
switch(curr_index){
case arr1_0:
case arr1_1:
case arr1_2:
curr_arr = my_struc.arr_1;
break;
case arr2_0:
case arr2_1:
curr_arr = my_struc.arr_2;
break;
}
EDIT:
For the index assignment part, you could do a second switch like this:
switch(unknown_index){
case arr1_0:
curr_index = arr1_0;
break;
case arr1_1:
curr_index = arr1_1;
break;
case arr1_2:
curr_index = arr1_2;
break;
case arr2_0:
curr_index = arr2_0;
break;
case arr2_1:
curr_index = arr2_1;
break;
}
But since you're always assigning whatever the value of unknown_index is, the above is the same as this:
curr_index = unknown_index;
One, no.
Two, just use ifs and elses. As the saying goes, when you have a hammer, everything looks like a nail. switch is a really weird "hammer" to try applying to everything.
Three, um, I guess you could use goto everywhere, but we decided this was a bad idea and creates horribly messes of code in the 80s or something.
Within a switch you have access to all the locally defined variables. I'm not quite sure I understand your question... it seems like what you're trying to do is best accomplished by 2 switches:
switch(unknown_index){
case(arr1_0):
case(arr1_1):
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
break;
case(arr2_0):
case(arr2_1):
curr_arr = my_struc.arr_2; //....
break;
}
switch(unknown_index){
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}

Any way to programmatically get the FPS of a video?

I am currently working in this problem for hours now. I have to create a program that when user gets a video from a child window which accesses your hard disk drives, I have to get the frame rate and other properties from that video.
Here's a sample code of how I'm getting the videos and some of their properties.
SelectDirectoryWindow selectDirectoryWindow = (sender as SelectDirectoryWindow);
if (selectDirectoryWindow.DialogResult.GetValueOrDefault(false))
{
foreach (System.IO.FileInfo fileInfo in selectDirectoryWindow.VideoFiles)
{
VideoFileInfo videoFileInfo = new VideoFileInfo();
videoFileInfo.FileName = fileInfo.Name;
videoFileInfo.Path = fileInfo.FullName;
videoFileInfo.Extension = fileInfo.Extension;
videoFileInfo.FileSize = fileInfo.Length;
switch (videoFileInfo.Extension.ToUpper())
{
case ".WMV":
videoFileInfo.VideoFileType = Constants.VideoFileType.Wmv;
break;
case ".MOV":
videoFileInfo.VideoFileType = Constants.VideoFileType.ProResHq;
break;
case ".MPG":
videoFileInfo.VideoFileType = Constants.VideoFileType.Mpeg2;
break;
case ".ISM":
videoFileInfo.VideoFileType = Constants.VideoFileType.SmoothStreaming;
break;
case ".MP4":
videoFileInfo.VideoFileType = Constants.VideoFileType.iPad;
break;
default:
break;
}
Is there any way I can also get the frame rate, video duration and bit rate from this? What can I do to get the frame rate and bit rate? Thanks in advance.
I have found the answer. There is a ShellFile class on the Microsoft.WindowsAPICodePack.Shell. In there you can get the properties of the video, just give it the source of the file(filepath). And you can get anything from there.
Here's how I got the Frame Rate.
ShellFile shellFile = ShellFile.FromFilePath(sourceFile);
return (shellFile.Properties.System.Video.FrameRate.Value / 1000).ToString();

Resources