Giter VIP home page Giter VIP logo

littlebigio's Introduction

littlebigIO

littlebigIO is a lightweight Java library for Java 17 or newer that provides I/O streams and bit-conversion methods to deal with primitive data types in big-endian and little-endian byte order. Java's standard classes, such as DataInputStream, are mostly laid out for network transmission, which supports big-endian data only. In some scenarios, we have to use little-endian, so this small library was created to fill this gap. The project can be imported and opened in Eclipse.

The library's most important classes are:

  • BinaryInputStream: An input stream to read primitive data types in a specified byte order from an underlying InputStream.
  • BinaryOutputStream: An output stream to write primitive data types in a specified byte order to an underlying OutputStream.
  • BitConverter: Consists of helper methods to convert between bytes and primitive data types, similar to .NET's BitConverter class.

Essentially, BinaryInputStream and BinaryOutputStream can be used like Java's DataInputStream and DataOutputStream, respectively.

Documentation

All javadocs for this library can be found on my website.

Example

This should help you get a basic idea of how to use the features of this library. Below is some example code showing how to read and write int array files. A byte order mark, or BOM for short, is written at the file's start, allowing us to detect the correct byte order while reading from the file.

	public int[] readIntArray(File f) throws IOException {
		try (BinaryInputStream in = new BinaryInputStream(new FileInputStream(f))) {
			// Read and verify BOM
			char bom = in.readChar();

			if (bom == 0xFFFE) {
				in.swapOrder();
			} else if (bom != 0xFEFF) {
				throw new BOMException("File does not start with UTF-16 BOM");
			}

			// Read int array
			int[] arr = new int[in.readInt()];

			for (int i = 0; i < arr.length; i++) {
				arr[i] = in.readInt();
			}

			return arr;
		}
	}

	public void writeIntArray(File f, int[] arr) throws IOException {
		try (BinaryOutputStream out = new BinaryOutputStream(new FileOutputStream(f))) {
			out.writeChar(0xFEFF);
			out.writeInt(arr.length);

			for (int i : arr) {
				out.writeInt(i);
			}
		}
	}

littlebigio's People

Contributors

sunakazekun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.