Skip to content

Commit 72b72aa

Browse files
committed
Minor code reorg for SmileParser
1 parent 57c4c25 commit 72b72aa

File tree

1 file changed

+61
-51
lines changed

1 file changed

+61
-51
lines changed

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParser.java

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,55 +2256,6 @@ protected final void _reportInvalidUnsignedVInt(int firstCh, int lastCh) throws
22562256
currentToken(), firstCh);
22572257
}
22582258

2259-
private final byte[] _read7BitBinaryWithLength() throws IOException
2260-
{
2261-
int byteLen = _readUnsignedVInt();
2262-
byte[] result = new byte[byteLen];
2263-
int ptr = 0;
2264-
int lastOkPtr = byteLen - 7;
2265-
2266-
// first, read all 7-by-8 byte chunks
2267-
while (ptr <= lastOkPtr) {
2268-
if ((_inputEnd - _inputPtr) < 8) {
2269-
_loadToHaveAtLeast(8);
2270-
}
2271-
int i1 = (_inputBuffer[_inputPtr++] << 25)
2272-
+ (_inputBuffer[_inputPtr++] << 18)
2273-
+ (_inputBuffer[_inputPtr++] << 11)
2274-
+ (_inputBuffer[_inputPtr++] << 4);
2275-
int x = _inputBuffer[_inputPtr++];
2276-
i1 += x >> 3;
2277-
int i2 = ((x & 0x7) << 21)
2278-
+ (_inputBuffer[_inputPtr++] << 14)
2279-
+ (_inputBuffer[_inputPtr++] << 7)
2280-
+ _inputBuffer[_inputPtr++];
2281-
// Ok: got our 7 bytes, just need to split, copy
2282-
result[ptr++] = (byte)(i1 >> 24);
2283-
result[ptr++] = (byte)(i1 >> 16);
2284-
result[ptr++] = (byte)(i1 >> 8);
2285-
result[ptr++] = (byte)i1;
2286-
result[ptr++] = (byte)(i2 >> 16);
2287-
result[ptr++] = (byte)(i2 >> 8);
2288-
result[ptr++] = (byte)i2;
2289-
}
2290-
// and then leftovers: n+1 bytes to decode n bytes
2291-
int toDecode = (result.length - ptr);
2292-
if (toDecode > 0) {
2293-
if ((_inputEnd - _inputPtr) < (toDecode+1)) {
2294-
_loadToHaveAtLeast(toDecode+1);
2295-
}
2296-
int value = _inputBuffer[_inputPtr++];
2297-
for (int i = 1; i < toDecode; ++i) {
2298-
value = (value << 7) + _inputBuffer[_inputPtr++];
2299-
result[ptr++] = (byte) (value >> (7 - i));
2300-
}
2301-
// last byte is different, has remaining 1 - 6 bits, right-aligned
2302-
value <<= toDecode;
2303-
result[ptr] = (byte) (value + _inputBuffer[_inputPtr++]);
2304-
}
2305-
return result;
2306-
}
2307-
23082259
/*
23092260
/**********************************************************
23102261
/* Internal methods, secondary String parsing
@@ -2484,16 +2435,23 @@ private final void _decodeLongUnicode() throws IOException
24842435
_textBuffer.setCurrentLength(outPtr);
24852436
}
24862437

2438+
/*
2439+
/**********************************************************************
2440+
/* Internal methods, secondary Binary data parsing
2441+
/**********************************************************************
2442+
*/
2443+
24872444
private final byte[] _finishRawBinary() throws IOException
24882445
{
24892446
int byteLen = _readUnsignedVInt();
24902447

24912448
// 20-Mar-2021, tatu [dataformats-binary#260]: avoid eager allocation
24922449
// for very large content
24932450
if (byteLen > LONGEST_NON_CHUNKED_BINARY) {
2494-
return _finishLongContiguousBytes(byteLen);
2451+
return _finishRawBinaryLong(byteLen);
24952452
}
24962453

2454+
// But use simpler, no intermediate buffering, for more compact cases
24972455
final int expLen = byteLen;
24982456
final byte[] b = new byte[byteLen];
24992457

@@ -2520,7 +2478,7 @@ private final byte[] _finishRawBinary() throws IOException
25202478
}
25212479

25222480
// @since 2.12.3
2523-
protected byte[] _finishLongContiguousBytes(final int expLen) throws IOException
2481+
protected byte[] _finishRawBinaryLong(final int expLen) throws IOException
25242482
{
25252483
int left = expLen;
25262484

@@ -2545,6 +2503,58 @@ protected byte[] _finishLongContiguousBytes(final int expLen) throws IOException
25452503
}
25462504
}
25472505

2506+
// Helper method for reading full contents of a 7-bit (7/8) encoded
2507+
// binary data chunk: starting with leading leading VInt length indicator
2508+
// followed by encoded data
2509+
private final byte[] _read7BitBinaryWithLength() throws IOException
2510+
{
2511+
int byteLen = _readUnsignedVInt();
2512+
byte[] result = new byte[byteLen];
2513+
int ptr = 0;
2514+
int lastOkPtr = byteLen - 7;
2515+
2516+
// first, read all 7-by-8 byte chunks
2517+
while (ptr <= lastOkPtr) {
2518+
if ((_inputEnd - _inputPtr) < 8) {
2519+
_loadToHaveAtLeast(8);
2520+
}
2521+
int i1 = (_inputBuffer[_inputPtr++] << 25)
2522+
+ (_inputBuffer[_inputPtr++] << 18)
2523+
+ (_inputBuffer[_inputPtr++] << 11)
2524+
+ (_inputBuffer[_inputPtr++] << 4);
2525+
int x = _inputBuffer[_inputPtr++];
2526+
i1 += x >> 3;
2527+
int i2 = ((x & 0x7) << 21)
2528+
+ (_inputBuffer[_inputPtr++] << 14)
2529+
+ (_inputBuffer[_inputPtr++] << 7)
2530+
+ _inputBuffer[_inputPtr++];
2531+
// Ok: got our 7 bytes, just need to split, copy
2532+
result[ptr++] = (byte)(i1 >> 24);
2533+
result[ptr++] = (byte)(i1 >> 16);
2534+
result[ptr++] = (byte)(i1 >> 8);
2535+
result[ptr++] = (byte)i1;
2536+
result[ptr++] = (byte)(i2 >> 16);
2537+
result[ptr++] = (byte)(i2 >> 8);
2538+
result[ptr++] = (byte)i2;
2539+
}
2540+
// and then leftovers: n+1 bytes to decode n bytes
2541+
int toDecode = (result.length - ptr);
2542+
if (toDecode > 0) {
2543+
if ((_inputEnd - _inputPtr) < (toDecode+1)) {
2544+
_loadToHaveAtLeast(toDecode+1);
2545+
}
2546+
int value = _inputBuffer[_inputPtr++];
2547+
for (int i = 1; i < toDecode; ++i) {
2548+
value = (value << 7) + _inputBuffer[_inputPtr++];
2549+
result[ptr++] = (byte) (value >> (7 - i));
2550+
}
2551+
// last byte is different, has remaining 1 - 6 bits, right-aligned
2552+
value <<= toDecode;
2553+
result[ptr] = (byte) (value + _inputBuffer[_inputPtr++]);
2554+
}
2555+
return result;
2556+
}
2557+
25482558
/*
25492559
/**********************************************************
25502560
/* Internal methods, skipping

0 commit comments

Comments
 (0)