How to Read Binary File With Nodejs

binary-file

NPM version Node version

Read and write binary types in files. This library allows you to use the functions y'all would utilize on a Buffer, like readUInt32, directly on files. Information technology is promises-based.

              npm install --save binary-file                          

Use case

Say you lot want to parse a simple binary file for an UInt32 containing the length of the string that follows. With binary-file, you tin only:

                

' employ strict ' ;

const BinaryFile = require ( ' binary-file ' ) ;

const myBinaryFile = new BinaryFile ( ' ./file.bin ' , ' r ' ) ;

myBinaryFile . open ( ) . then ( role ( ) {

console . log ( ' File opened ' ) ;

return myBinaryFile . readUInt32 ( ) ;

} ) . so ( function ( stringLength ) {

render myBinaryFile . readString ( stringLength ) ;

} ) . so ( role ( string ) {

console . log ( ` File read: ${ string } ` ) ;

return myBinaryFile . close ( ) ;

} ) . so ( function ( ) {

console . log ( ' File airtight ' ) ;

} ) . catch ( function ( err ) {

console . log ( ` There was an error: ${ err } ` ) ;

} ) ;

And information technology looks even amend with ES7 async / wait:

                

' use strict ' ;

const BinaryFile = crave ( ' binary-file ' ) ;

const myBinaryFile = new BinaryFile ( ' ./file.bin ' , ' r ' ) ;

( async function ( ) {

try {

expect myBinaryFile . open ( ) ;

console . log ( ' File opened ' ) ;

const stringLength = await myBinaryFile . readUInt32 ( ) ;

const cord = await myBinaryFile . readString ( stringLength ) ;

console . log ( ` File read: ${ cord } ` ) ;

await myBinaryFile . close ( ) ;

panel . log ( ' File closed ' ) ;

} catch ( err ) {

console . log ( ` There was an mistake: ${ err } ` ) ;

}

} ) ( ) ;

You don't have to create a Buffer to write the data read from the file, y'all don't take to remember the position of the cursor: everything is handled by BinaryFile.

API

File

new BinaryFile(path, way, littleEndian = imitation)

Create a new case of BinaryFile.

  • path: the path of the file to open up
  • fashion: the manner in which to open up the file. Run across fs.open
  • littleEndian: endianness of the file

.open up()

Open the file.

Render a promise.

fulfilled when the file is opened

.size()

Go the size in bytes of the file.

Return a promise.

fulfilled with the size of the file in bytes

.tell()

Get the position of the cursor in the file.

Return the position of the cursor in the file.

.seek(position)

Set the position of the cursor in the file

  • position: the position where to place the cursor

Return the new position.

.close()

Close the file.

Return a hope.

fulfilled when the file is closed

Read

.read(length, position = zippo)

Read a Buffer in the file.

  • length: the number of bytes to read
  • position: the position where to read the Buffer in the file. If not prepare, information technology will employ the internal cursor. If set, the internal cursor won't move

Return a promise.

fulfilled with the Buffer when the reading is washed

.readInt8(position = zilch),

.readUInt8(position = null),

.readInt16(position = null),

.readUInt16(position = null),

.readInt32(position = nada),

.readUInt32(position = aught),

.readInt64(position = null),

.readUInt64(position = zilch),

.readFloat(position = cipher),

.readDouble(position = null)

Read a binary type in the file.

  • position: the position where to read the binary type in the file. If non fix, it volition utilize the internal cursor. If fix, the internal cursor won't move

Return a promise.

fulfilled with the value read when the reading is done

.readString(length, position = zippo)

Read a string in the file.

  • length: the number of bytes of the string to read
  • position: the position where to read the string in the file. If not set, it volition use the internal cursor. If fix, the internal cursor won't motion

Render a promise.

fulfilled with the string read when the reading is done

Write

.write(buffer, position = zippo)

Read a Buffer in the file.

  • buffer: the Buffer to write
  • position: the position where to write the Buffer in the file. If not prepare, it will use the internal cursor. If ready, the internal cursor won't move

Render a promise.

fulfilled with the number of bytes written when the writing is done

.writeInt8(value, position = goose egg),

.writeUInt8(value, position = null),

.writeInt16(value, position = aught),

.writeUInt16(value, position = aught),

.writeInt32(value, position = null),

.writeUInt32(value, position = null),

.writeInt64(value, position = cipher),

.writeUInt64(value, position = null),

.writeFloat(value, position = null),

.writeDouble(value, position = null)

Write a binary blazon in the file.

  • value: the value to write in the corresponding binary blazon
  • position: the position where to write the binary type in the file. If non set, it will use the internal cursor. If set, the internal cursor won't move

Return a hope.

fulfilled with the number of bytes written when the writing is done

.writeString(string, position = null)

Write a string in the file.

  • string: the string to write
  • position: the position where to write the string in the file. If not set, it will use the internal cursor. If set, the internal cursor won't motility

Return a promise.

fulfilled with the number of bytes written when the writing is done

jamescampenish.blogspot.com

Source: https://www.npmjs.com/package/binary-file

0 Response to "How to Read Binary File With Nodejs"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel