use of java.nio.BufferUnderflowException in project jvm-serializers by eishay.
the class Media method unmarshal.
/**
* Deserializes the object.
* @param buf the data source.
* @param offset the initial index for {@code buf}, inclusive.
* @return the final index for {@code buf}, exclusive.
* @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
* @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
* @throws InputMismatchException when the data does not match this object's schema.
*/
public int unmarshal(byte[] buf, int offset) {
int i = offset;
try {
byte header = buf[i++];
if (header == (byte) 0) {
int size = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
size |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
if (size > colferSizeMax)
throw new SecurityException(format("colfer: field serializers/colfer/media.media.uri size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
int start = i;
i += size;
this.uri = new String(buf, start, size, this._utf8);
header = buf[i++];
}
if (header == (byte) 1) {
int size = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
size |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
if (size > colferSizeMax)
throw new SecurityException(format("colfer: field serializers/colfer/media.media.title size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
int start = i;
i += size;
this.title = new String(buf, start, size, this._utf8);
header = buf[i++];
}
if (header == (byte) 2) {
int x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
x |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
this.width = x;
header = buf[i++];
} else if (header == (byte) (2 | 0x80)) {
int x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
x |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
this.width = -x;
header = buf[i++];
}
if (header == (byte) 3) {
int x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
x |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
this.height = x;
header = buf[i++];
} else if (header == (byte) (3 | 0x80)) {
int x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
x |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
this.height = -x;
header = buf[i++];
}
if (header == (byte) 4) {
int size = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
size |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
if (size > colferSizeMax)
throw new SecurityException(format("colfer: field serializers/colfer/media.media.format size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
int start = i;
i += size;
this.format = new String(buf, start, size, this._utf8);
header = buf[i++];
}
if (header == (byte) 5) {
long x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
if (shift == 56 || b >= 0) {
x |= (b & 0xffL) << shift;
break;
}
x |= (b & 0x7fL) << shift;
}
this.duration = x;
header = buf[i++];
} else if (header == (byte) (5 | 0x80)) {
long x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
if (shift == 56 || b >= 0) {
x |= (b & 0xffL) << shift;
break;
}
x |= (b & 0x7fL) << shift;
}
this.duration = -x;
header = buf[i++];
}
if (header == (byte) 6) {
long x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
if (shift == 56 || b >= 0) {
x |= (b & 0xffL) << shift;
break;
}
x |= (b & 0x7fL) << shift;
}
this.size = x;
header = buf[i++];
} else if (header == (byte) (6 | 0x80)) {
long x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
if (shift == 56 || b >= 0) {
x |= (b & 0xffL) << shift;
break;
}
x |= (b & 0x7fL) << shift;
}
this.size = -x;
header = buf[i++];
}
if (header == (byte) 7) {
int length = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
length |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
if (length > colferListMax)
throw new SecurityException(format("colfer: field serializers/colfer/media.media.persons length %d exceeds %d elements", length, colferListMax));
String[] a = new String[length];
for (int ai = 0; ai < length; ai++) {
int size = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
size |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
if (size > colferSizeMax)
throw new SecurityException(format("colfer: field serializers/colfer/media.media.persons size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
int start = i;
i += size;
a[ai] = new String(buf, start, size, this._utf8);
}
this.persons = a;
header = buf[i++];
}
if (header == (byte) 8) {
int size = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
size |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
if (size > colferSizeMax)
throw new SecurityException(format("colfer: field serializers/colfer/media.media.copyright size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
int start = i;
i += size;
this.copyright = new String(buf, start, size, this._utf8);
header = buf[i++];
}
if (header == (byte) 9) {
int x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
x |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
this.bitrate = x;
header = buf[i++];
} else if (header == (byte) (9 | 0x80)) {
int x = 0;
for (int shift = 0; true; shift += 7) {
byte b = buf[i++];
x |= (b & 0x7f) << shift;
if (shift == 28 || b >= 0)
break;
}
this.bitrate = -x;
header = buf[i++];
}
if (header == (byte) 10) {
this.hasBitrate = true;
header = buf[i++];
}
if (header == (byte) 11) {
this.flashPlay = true;
header = buf[i++];
}
if (header == (byte) 12) {
this.javaPlay = true;
header = buf[i++];
}
if (header != (byte) 0x7f)
throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
} catch (IndexOutOfBoundsException e) {
if (i - offset > colferSizeMax)
throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
if (i >= buf.length)
throw new BufferUnderflowException();
throw new RuntimeException("colfer: bug", e);
}
if (i - offset > colferSizeMax)
throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
return i;
}
use of java.nio.BufferUnderflowException in project buck by facebook.
the class NulTerminatedCharsetDecoder method decodeString.
@SuppressWarnings("PMD.PrematureDeclaration")
public String decodeString(ByteBuffer in) throws CharacterCodingException {
reset();
int startPosition = in.position();
int nulOffset = findNulOffset(in);
if (nulOffset == in.limit()) {
throw new BufferUnderflowException();
}
int charBufferNeeded = (int) ((nulOffset - startPosition) * decoder.averageCharsPerByte());
if (charBuffer.capacity() < charBufferNeeded) {
charBuffer = CharBuffer.allocate(charBufferNeeded);
} else {
charBuffer.clear();
}
StringBuilder sb = new StringBuilder();
while (true) {
Result result = decodeChunk(in, nulOffset, charBuffer, true);
if (result.coderResult.isError()) {
result.coderResult.throwException();
}
charBuffer.flip();
sb.append(charBuffer);
charBuffer.compact();
if (result.nulTerminatorReached || !in.hasRemaining()) {
break;
}
}
return sb.toString();
}
use of java.nio.BufferUnderflowException in project buck by facebook.
the class ObjectFileScrubbers method createDateUidGidScrubber.
public static FileContentsScrubber createDateUidGidScrubber(final PaddingStyle paddingStyle) {
return new FileContentsScrubber() {
/**
* Efficiently modifies the archive backed by the given buffer to remove any non-deterministic
* meta-data such as timestamps, UIDs, and GIDs.
*/
@SuppressWarnings("PMD.AvoidUsingOctalValues")
@Override
public void scrubFile(FileChannel file) throws IOException, ScrubException {
try {
ByteBuffer header = ByteBuffer.allocate(GLOBAL_HEADER_SIZE);
file.read(header);
// Grab the global header chunk and verify it's accurate.
header.position(0);
byte[] globalHeader = getBytes(header, GLOBAL_HEADER_SIZE);
boolean thin = checkHeader(globalHeader);
// Iterate over all the file meta-data entries, injecting zero's for timestamp,
// UID, and GID.
final int entrySize = 16 + /* fileName */
12 + /* file modification time */
6 + /* owner ID */
6 + /* group ID */
8 + /* file mode */
10 + /* file size */
2;
long start = GLOBAL_HEADER_SIZE;
ByteBuffer buffer = ByteBuffer.allocate(entrySize);
while (start < file.size()) {
checkArchive(file.size() - start >= entrySize, "Invalid entry metadata format");
buffer.clear();
file.position(start);
int read = file.read(buffer);
checkArchive(read == entrySize, "Not all bytes have been read");
// position points just past the last byte read, so need to reset
buffer.position(0);
String fileName = new String(getBytes(buffer, 16), Charsets.US_ASCII).trim();
// Inject 0's for the non-deterministic meta-data entries.
/* File modification timestamp */
putIntAsDecimalString(buffer, 12, ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP, paddingStyle);
/* Owner ID */
putIntAsDecimalString(buffer, 6, 0, paddingStyle);
/* Group ID */
putIntAsDecimalString(buffer, 6, 0, paddingStyle);
/* File mode */
putIntAsOctalString(buffer, 8, 0100644, paddingStyle);
long fileSize = getDecimalStringAsLong(buffer, 10);
// Lastly, grab the file magic entry and verify it's accurate.
byte[] fileMagic = getBytes(buffer, 2);
checkArchive(Arrays.equals(END_OF_FILE_HEADER_MARKER, fileMagic), "invalid file magic");
// write the changes
// position points just past the last byte accessed, need to reset
buffer.position(0);
file.position(start);
int written = file.write(buffer);
checkArchive(written == entrySize, "Not all bytes have been written");
// Skip the file data.
start += entrySize;
if (!thin || SPECIAL_ENTRIES.contains(fileName)) {
start += fileSize + fileSize % 2;
}
}
// Convert any low-level exceptions to `ArchiveExceptions`s.
} catch (BufferUnderflowException | ReadOnlyBufferException e) {
throw new ScrubException(e.getMessage());
}
}
};
}
use of java.nio.BufferUnderflowException in project cassandra by apache.
the class MapSerializer method getSerializedValue.
/**
* Given a serialized map, gets the value associated with a given key.
* @param serializedMap a serialized map
* @param serializedKey a serialized key
* @param keyType the key type for the map
* @return the value associated with the key if one exists, null otherwise
*/
public ByteBuffer getSerializedValue(ByteBuffer serializedMap, ByteBuffer serializedKey, AbstractType keyType) {
try {
ByteBuffer input = serializedMap.duplicate();
int n = readCollectionSize(input, ProtocolVersion.V3);
for (int i = 0; i < n; i++) {
ByteBuffer kbb = readValue(input, ProtocolVersion.V3);
ByteBuffer vbb = readValue(input, ProtocolVersion.V3);
int comparison = keyType.compare(kbb, serializedKey);
if (comparison == 0)
return vbb;
else if (comparison > 0)
// since the map is in sorted order, we know we've gone too far and the element doesn't exist
return null;
}
return null;
} catch (BufferUnderflowException e) {
throw new MarshalException("Not enough bytes to read a map");
}
}
use of java.nio.BufferUnderflowException in project cassandra by apache.
the class SetSerializer method deserializeForNativeProtocol.
public Set<T> deserializeForNativeProtocol(ByteBuffer bytes, ProtocolVersion version) {
try {
ByteBuffer input = bytes.duplicate();
int n = readCollectionSize(input, version);
if (n < 0)
throw new MarshalException("The data cannot be deserialized as a set");
// If the received bytes are not corresponding to a set, n might be a huge number.
// In such a case we do not want to initialize the set with that initialCapacity as it can result
// in an OOM when add is called (see CASSANDRA-12618). On the other hand we do not want to have to resize
// the set if we can avoid it, so we put a reasonable limit on the initialCapacity.
Set<T> l = new LinkedHashSet<T>(Math.min(n, 256));
for (int i = 0; i < n; i++) {
ByteBuffer databb = readValue(input, version);
elements.validate(databb);
l.add(elements.deserialize(databb));
}
if (input.hasRemaining())
throw new MarshalException("Unexpected extraneous bytes after set value");
return l;
} catch (BufferUnderflowException e) {
throw new MarshalException("Not enough bytes to read a set");
}
}
Aggregations