isbinaryfile

WebJar for isbinaryfile

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

isbinaryfile
Last Version

Last Version

4.0.0-rc1
Release Date

Release Date

Type

Type

jar
Description

Description

isbinaryfile
WebJar for isbinaryfile
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/gjtorikian/isBinaryFile

Download isbinaryfile

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/isbinaryfile/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>isbinaryfile</artifactId>
    <version>4.0.0-rc1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/isbinaryfile/
implementation 'org.webjars.npm:isbinaryfile:4.0.0-rc1'
// https://jarcasting.com/artifacts/org.webjars.npm/isbinaryfile/
implementation ("org.webjars.npm:isbinaryfile:4.0.0-rc1")
'org.webjars.npm:isbinaryfile:jar:4.0.0-rc1'
<dependency org="org.webjars.npm" name="isbinaryfile" rev="4.0.0-rc1">
  <artifact name="isbinaryfile" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='isbinaryfile', version='4.0.0-rc1')
)
libraryDependencies += "org.webjars.npm" % "isbinaryfile" % "4.0.0-rc1"
[org.webjars.npm/isbinaryfile "4.0.0-rc1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

isBinaryFile

Detects if a file is binary in Node.js using promises. Similar to Perl's -B switch, in that:

  • it reads the first few thousand bytes of a file
  • checks for a null byte; if it's found, it's binary
  • flags non-ASCII characters. After a certain number of "weird" characters, the file is flagged as binary

Much of the logic is pretty much ported from ag.

Note: if the file doesn't exist or is a directory, an error is thrown.

Installation

npm install isbinaryfile

Usage

Returns Promise<boolean> (or just boolean for *Sync). true if the file is binary, false otherwise.

isBinaryFile(filepath)

  • filepath - a string indicating the path to the file.

isBinaryFile(bytes[, size])

  • bytes - a Buffer of the file's contents.
  • size - an optional number indicating the file size.

isBinaryFileSync(filepath)

  • filepath - a string indicating the path to the file.

isBinaryFileSync(bytes[, size])

  • bytes - a Buffer of the file's contents.
  • size - an optional number indicating the file size.

Examples

Here's an arbitrary usage:

const isBinaryFile = require("isbinaryfile").isBinaryFile;
const fs = require("fs");

const filename = "fixtures/pdf.pdf";
const data = fs.readFileSync(filename);
const stat = fs.lstatSync(filename);

isBinaryFile(data, stat.size).then((result) => {
  if (result) {
    console.log("It is binary!")
  }
  else {
    console.log("No it is not.")
  }
});

const isBinaryFileSync = require("isbinaryfile").isBinaryFileSync;
const bytes = fs.readFileSync(filename);
const size = fs.lstatSync(filename).size;
console.log(isBinaryFileSync(bytes, size)); // true or false

Testing

Run npm install, then run npm test.

Versions

Version
4.0.0-rc1
3.0.3
3.0.2
3.0.0