io.permazen.encoding.ByteArrayEncoding Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of permazen-encoding Show documentation
Show all versions of permazen-encoding Show documentation
Permazen classes for encoding Java values to/from binary representations.
The newest version!
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package io.permazen.encoding;
import com.google.common.primitives.Bytes;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
/**
* {@code byte[]} primitive array type. Does not support null arrays.
*/
public class ByteArrayEncoding extends IntegralArrayEncoding {
private static final long serialVersionUID = -5978203098536001843L;
public ByteArrayEncoding() {
super(new ByteEncoding(null), byte[].class);
}
@Override
protected int getArrayLength(byte[] array) {
return array.length;
}
@Override
protected Byte getArrayElement(byte[] array, int index) {
return array[index];
}
@Override
protected byte[] createArray(List elements) {
return Bytes.toArray(elements);
}
@Override
protected void encode(byte[] array, DataOutputStream output) throws IOException {
output.write(array, 0, array.length);
}
@Override
protected byte[] decode(DataInputStream input, int numBytes) throws IOException {
final byte[] data = new byte[numBytes];
input.readFully(data);
return data;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy