|
1 | | -# PocketNode-BinaryStream |
2 | | -PocketNode Binary Stream Module |
| 1 | +# BinaryStream TypeScript Package |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`BinaryStream` is a TypeScript package designed to facilitate the reading and writing of binary data. This package leverages the `ArrayBuffer` and `DataView` interfaces, making it compatible with both Node.js and browser environments. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Supports both Node.js and browser environments. |
| 10 | +- Provides an easy-to-use API for handling binary data. |
| 11 | +- Utilizes `ArrayBuffer` and `DataView` for efficient binary data manipulation. |
| 12 | +- Methods for reading and writing various data types (integers, floats, strings, etc.). |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +You can install the `BinaryStream` package via npm: |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install @pocketnode/binarystream |
| 20 | +``` |
| 21 | + |
| 22 | +or |
| 23 | + |
| 24 | +```bash |
| 25 | +bun install @pocketnode/binarystream |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### Importing the Module |
| 31 | + |
| 32 | +In a TypeScript or JavaScript file, import the `BinaryStream` module: |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { BinaryStream } from "binarystream"; |
| 36 | +``` |
| 37 | + |
| 38 | +### Creating a BinaryStream Instance |
| 39 | + |
| 40 | +To create a new instance of `BinaryStream`, you can either provide an existing `ArrayBuffer` or specify the size of a new buffer: |
| 41 | + |
| 42 | +```typescript |
| 43 | +// Create a BinaryStream with a new ArrayBuffer of 1024 bytes |
| 44 | +const stream = new BinaryStream(1024); |
| 45 | + |
| 46 | +// Create a BinaryStream from an existing ArrayBuffer |
| 47 | +const buffer = new ArrayBuffer(1024); |
| 48 | +const streamFromBuffer = new BinaryStream(buffer); |
| 49 | +``` |
| 50 | + |
| 51 | +### Writing Data |
| 52 | + |
| 53 | +The `BinaryStream` class provides methods to write various data types: |
| 54 | + |
| 55 | +```typescript |
| 56 | +stream.writeUint8(255); // Write an unsigned 8-bit integer |
| 57 | +stream.writeInt16(-32768); // Write a signed 16-bit integer |
| 58 | +stream.writeFloat32(3.14); // Write a 32-bit float |
| 59 | +stream.writeString("Hello"); // Write a string |
| 60 | +``` |
| 61 | + |
| 62 | +### Reading Data |
| 63 | + |
| 64 | +Similarly, you can read data using corresponding methods: |
| 65 | + |
| 66 | +```typescript |
| 67 | +const uint8 = stream.readUint8(); // Read an unsigned 8-bit integer |
| 68 | +const int16 = stream.readInt16(); // Read a signed 16-bit integer |
| 69 | +const float32 = stream.readFloat32(); // Read a 32-bit float |
| 70 | +const str = stream.readString(); // Read a string |
| 71 | +``` |
| 72 | + |
| 73 | +### Example |
| 74 | + |
| 75 | +Here is a complete example demonstrating how to write and read data using `BinaryStream`: |
| 76 | + |
| 77 | +```typescript |
| 78 | +import { BinaryStream } from "binarystream"; |
| 79 | + |
| 80 | +// Create a BinaryStream with a new ArrayBuffer of 1024 bytes |
| 81 | +const stream = new BinaryStream(1024); |
| 82 | + |
| 83 | +// Write data to the stream |
| 84 | +stream.writeUint8(255); |
| 85 | +stream.writeInt16(-32768); |
| 86 | +stream.writeFloat32(3.14); |
| 87 | +stream.writeString("Hello, BinaryStream!"); |
| 88 | + |
| 89 | +// Reset the position to the beginning of the stream for reading |
| 90 | +stream.position = 0; |
| 91 | + |
| 92 | +// Read data from the stream |
| 93 | +const uint8 = stream.readUint8(); |
| 94 | +const int16 = stream.readInt16(); |
| 95 | +const float32 = stream.readFloat32(); |
| 96 | +const str = stream.readString(); |
| 97 | + |
| 98 | +console.log(uint8); // 255 |
| 99 | +console.log(int16); // -32768 |
| 100 | +console.log(float32); // 3.14 |
| 101 | +console.log(str); // Hello, BinaryStream! |
| 102 | +``` |
| 103 | + |
| 104 | +## API Documentation |
| 105 | + |
| 106 | +### BinaryStream |
| 107 | + |
| 108 | +#### Constructor |
| 109 | + |
| 110 | +- `new BinaryStream(size: number): BinaryStream` |
| 111 | +- `new BinaryStream(buffer: ArrayBuffer): BinaryStream` |
| 112 | + |
| 113 | +#### Methods |
| 114 | + |
| 115 | +- `writeUint8(value: number): void` |
| 116 | +- `writeInt16(value: number): void` |
| 117 | +- `writeFloat32(value: number): void` |
| 118 | +- `writeString(value: string): void` |
| 119 | + |
| 120 | +- `readUint8(): number` |
| 121 | +- `readInt16(): number` |
| 122 | +- `readFloat32(): number` |
| 123 | +- `readString(): string` |
| 124 | + |
| 125 | +#### Properties |
| 126 | + |
| 127 | +- `position: number` - The current read/write position in the buffer. |
| 128 | +- `buffer: ArrayBuffer` - The underlying `ArrayBuffer`. |
| 129 | + |
| 130 | +## Compatibility |
| 131 | + |
| 132 | +`BinaryStream` works in both Node.js and browser environments. It relies on `ArrayBuffer` and `DataView`, which are available in modern browsers and Node.js. |
| 133 | + |
| 134 | +## License |
| 135 | + |
| 136 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 137 | + |
| 138 | +## Contributing |
| 139 | + |
| 140 | +Contributions are welcome! Please read the [CONTRIBUTING](CONTRIBUTING.md) guidelines for more information. |
| 141 | + |
| 142 | +## Contact |
| 143 | + |
| 144 | +For questions or feedback, please open an issue on the [GitHub repository](https://github.com/yourusername/binarystream). |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +Thank you for using `BinaryStream`! We hope it simplifies your binary data handling tasks. |
0 commit comments