bytes-core

Provides universal interface for having an immutable representation of sequence of bytes

License

License

MIT
GroupId

GroupId

com.avast.bytes
ArtifactId

ArtifactId

bytes-core
Last Version

Last Version

2.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

bytes-core
Provides universal interface for having an immutable representation of sequence of bytes
Project URL

Project URL

https://github.com/avast/bytes
Project Organization

Project Organization

Avast
Source Code Management

Source Code Management

https://github.com/avast/bytes

Download bytes-core

How to add to project

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

Dependencies

test (4)

Group / Artifact Type Version
junit : junit jar 4.12
com.novocode : junit-interface jar 0.11
ch.qos.logback : logback-classic jar 1.2.3
javax.annotation : javax.annotation-api jar 1.3.2

Project Modules

There are no modules declared in this project.

Bytes

Build Version

This library contains an immutable representation of sequence of bytes: com.avast.bytes.Bytes.

The goal is to provide single immutable representation of bytes that is not dependent on a third-party library (like com.google.protobuf.ByteString). However, multiple implementations of the Bytes interface can be created using third-party libraries to take advantage of their benefits (io.netty.buffer.ByteBuf for example). Implementations that require third-party dependencies will be separated to their own module, while the core module provides only the API and implementations based on Java standard library.

Features of Bytes include:

  • reading encapsulated data using random (Bytes.byteAt(int)) and linear (Bytes.newInputStream()) access
  • zero copy substring views (Bytes.view(int, int))
  • zero copy concat (Bytes.concat(otherBytes))
  • utility methods for reading & writing hex strings (Bytes.copyFromHex(someBytes.toHexString))
  • reading directly from InputStream (in some implementations, e.g. ByteArrayBytes.readFrom(is) or ByteArrayBytes.readFrom(is, offset, length))

Usage

Parsing Google Protocol Buffer message: Assume NotificationData is a class generated by protoc.

Bytes bytes = ...

try (InputStream bytesStream = bytes.newInputStream()) {
    NotificationData message = NotificationData.parseFrom(bytesStream);
}

Serializing Google Protocol Buffer message to Bytes:

NotificationData message = ...
try (Bytes.BuilderStream builder = ByteBufBytes.newBuilder(message.getSerializedSize())) {
    message.writeTo(builder);
    Bytes bytes = builder.toBytes();
}

...or serialize the message to ByteString as usual and wrap it with ByteStringBytes.

JDK-based implementations

The core module contains two implementations:

  • ByteArrayBytes - backed by byte[]
  • ByteBufferBytes - backed by java.nio.ByteBuffer
ByteArrayBytes bab = ByteArrayBytes.copyFrom(new byte[]{0, 0, 7});

ByteBufferBytes bbb = ByteBufferBytes.copyFrom(ByteBuffer.allocate(1024)); // the most useless buffer ever

com.google.protobuf.ByteString wrapper

The gpb module provides ByteStringBytes that implements Bytes by wrapping com.google.protobuf.ByteString.

ByteStringBytes bsb = ByteStringBytes.wrap(ByteString.copyFromUtf8("foo"));
com.avast.bytes

Avast

https://avast.github.io

Versions

Version
2.2.0
2.1.2
2.1.1
2.1.0