Issue #9554 - rename NBitIntegerParser and NBitStringParser to decoders

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2023-05-04 09:31:20 +10:00
parent 2d935efe4d
commit 03d6afae91
10 changed files with 86 additions and 86 deletions

View File

@ -18,7 +18,7 @@ import java.nio.ByteBuffer;
/**
* Used to decode integers as described in RFC7541.
*/
public class NBitIntegerParser
public class NBitIntegerDecoder
{
private int _prefix;
private long _total;

View File

@ -27,9 +27,9 @@ import org.eclipse.jetty.util.CharsetStringBuilder;
* <p>Characters which are illegal field-vchar values are replaced with
* either ' ' or '?' as described in RFC9110</p>
*/
public class NBitStringParser
public class NBitStringDecoder
{
private final NBitIntegerParser _integerParser;
private final NBitIntegerDecoder _integerDecoder;
private final HuffmanDecoder _huffmanBuilder;
private final CharsetStringBuilder.Iso88591StringBuilder _builder;
private boolean _huffman;
@ -46,9 +46,9 @@ public class NBitStringParser
VALUE
}
public NBitStringParser()
public NBitStringDecoder()
{
_integerParser = new NBitIntegerParser();
_integerDecoder = new NBitIntegerDecoder();
_huffmanBuilder = new HuffmanDecoder();
_builder = new CharsetStringBuilder.Iso88591StringBuilder();
}
@ -84,11 +84,11 @@ public class NBitStringParser
byte firstByte = buffer.get(buffer.position());
_huffman = ((0x80 >>> (8 - _prefix)) & firstByte) != 0;
_state = State.LENGTH;
_integerParser.setPrefix(_prefix - 1);
_integerDecoder.setPrefix(_prefix - 1);
continue;
case LENGTH:
_length = _integerParser.decodeInt(buffer);
_length = _integerDecoder.decodeInt(buffer);
if (_length < 0)
return null;
_state = State.VALUE;
@ -122,7 +122,7 @@ public class NBitStringParser
public void reset()
{
_state = State.PARSING;
_integerParser.reset();
_integerDecoder.reset();
_huffmanBuilder.reset();
_builder.reset();
_prefix = 0;

View File

@ -15,7 +15,7 @@ package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.junit.jupiter.api.Test;
@ -29,7 +29,7 @@ public class NBitIntegerParserTest
@Test
public void testParsingOverByteBoundary()
{
NBitIntegerParser parser = new NBitIntegerParser();
NBitIntegerDecoder decoder = new NBitIntegerDecoder();
String encoded = "FFBA09";
byte[] bytes = TypeUtil.fromHexString(encoded);
@ -37,11 +37,11 @@ public class NBitIntegerParserTest
ByteBuffer buffer1 = BufferUtil.toBuffer(bytes, 0, 2);
ByteBuffer buffer2 = BufferUtil.toBuffer(bytes, 2, 1);
parser.setPrefix(7);
int value = parser.decodeInt(buffer1);
decoder.setPrefix(7);
int value = decoder.decodeInt(buffer1);
assertThat(value, is(-1));
value = parser.decodeInt(buffer2);
value = decoder.decodeInt(buffer2);
assertThat(value, is(1337));
}
}

View File

@ -15,8 +15,8 @@ package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.http.compression.NBitIntegerEncoder;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.junit.jupiter.api.Test;
@ -26,7 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
@SuppressWarnings("PointlessArithmeticExpression")
public class NBitIntegerTest
{
private final NBitIntegerParser _parser = new NBitIntegerParser();
private final NBitIntegerDecoder _decoder = new NBitIntegerDecoder();
@Test
public void testOctetsNeeded()
@ -125,8 +125,8 @@ public class NBitIntegerTest
public void testDecode(int n, int expected, String encoded)
{
ByteBuffer buf = ByteBuffer.wrap(TypeUtil.fromHexString(encoded));
_parser.setPrefix(n);
assertEquals(expected, _parser.decodeInt(buf));
_decoder.setPrefix(n);
assertEquals(expected, _decoder.decodeInt(buf));
}
@Test
@ -149,8 +149,8 @@ public class NBitIntegerTest
{
ByteBuffer buf = ByteBuffer.wrap(TypeUtil.fromHexString("77EaFF"));
buf.position(1);
_parser.setPrefix(5);
assertEquals(10, _parser.decodeInt(buf));
_decoder.setPrefix(5);
assertEquals(10, _decoder.decodeInt(buf));
}
@Test
@ -173,8 +173,8 @@ public class NBitIntegerTest
{
ByteBuffer buf = ByteBuffer.wrap(TypeUtil.fromHexString("881f9a0aff"));
buf.position(1);
_parser.setPrefix(5);
assertEquals(1337, _parser.decodeInt(buf));
_decoder.setPrefix(5);
assertEquals(1337, _decoder.decodeInt(buf));
}
@Test
@ -197,7 +197,7 @@ public class NBitIntegerTest
{
ByteBuffer buf = ByteBuffer.wrap(TypeUtil.fromHexString("882aFf"));
buf.position(1);
_parser.setPrefix(8);
assertEquals(42, _parser.decodeInt(buf));
_decoder.setPrefix(8);
assertEquals(42, _decoder.decodeInt(buf));
}
}

View File

@ -21,7 +21,7 @@ import org.eclipse.jetty.http.HttpTokens;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http.compression.EncodingException;
import org.eclipse.jetty.http.compression.HuffmanDecoder;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.http2.hpack.HpackContext.Entry;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.CharsetStringBuilder;
@ -41,7 +41,7 @@ public class HpackDecoder
private final HpackContext _context;
private final MetaDataBuilder _builder;
private final HuffmanDecoder _huffmanDecoder;
private final NBitIntegerParser _integerParser;
private final NBitIntegerDecoder _integerDecoder;
private int _localMaxDynamicTableSize;
/**
@ -54,7 +54,7 @@ public class HpackDecoder
_localMaxDynamicTableSize = localMaxDynamicTableSize;
_builder = new MetaDataBuilder(maxHeaderSize);
_huffmanDecoder = new HuffmanDecoder();
_integerParser = new NBitIntegerParser();
_integerDecoder = new NBitIntegerDecoder();
}
public HpackContext getHpackContext()
@ -281,8 +281,8 @@ public class HpackDecoder
if (prefix != 8)
buffer.position(buffer.position() - 1);
_integerParser.setPrefix(prefix);
int decodedInt = _integerParser.decodeInt(buffer);
_integerDecoder.setPrefix(prefix);
int decodedInt = _integerDecoder.decodeInt(buffer);
if (decodedInt < 0)
throw new EncodingException("invalid integer encoding");
return decodedInt;
@ -295,7 +295,7 @@ public class HpackDecoder
}
finally
{
_integerParser.reset();
_integerDecoder.reset();
}
}

View File

@ -18,7 +18,7 @@ import java.nio.ByteBuffer;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.compression.EncodingException;
import org.eclipse.jetty.http.compression.HuffmanDecoder;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.http2.hpack.HpackContext.Entry;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
@ -53,12 +53,12 @@ public class HpackContextTest
if (prefix != 8)
buffer.position(buffer.position() - 1);
NBitIntegerParser parser = new NBitIntegerParser();
parser.setPrefix(prefix);
int decodedInt = parser.decodeInt(buffer);
NBitIntegerDecoder decoder = new NBitIntegerDecoder();
decoder.setPrefix(prefix);
int decodedInt = decoder.decodeInt(buffer);
if (decodedInt < 0)
throw new EncodingException("invalid integer encoding");
parser.reset();
decoder.reset();
return decodedInt;
}

View File

@ -24,7 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.http3.qpack.internal.QpackContext;
import org.eclipse.jetty.http3.qpack.internal.instruction.InsertCountIncrementInstruction;
import org.eclipse.jetty.http3.qpack.internal.instruction.SectionAcknowledgmentInstruction;
@ -52,7 +52,7 @@ public class QpackDecoder implements Dumpable
private final QpackContext _context;
private final DecoderInstructionParser _parser;
private final List<EncodedFieldSection> _encodedFieldSections = new ArrayList<>();
private final NBitIntegerParser _integerDecoder = new NBitIntegerParser();
private final NBitIntegerDecoder _integerDecoder = new NBitIntegerDecoder();
private final InstructionHandler _instructionHandler = new InstructionHandler();
private final Map<Long, AtomicInteger> _blockedStreams = new HashMap<>();
private int _maxHeaderSize;

View File

@ -16,8 +16,8 @@ package org.eclipse.jetty.http3.qpack.internal.parser;
import java.nio.ByteBuffer;
import org.eclipse.jetty.http.compression.EncodingException;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.http.compression.NBitStringParser;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.http.compression.NBitStringDecoder;
import org.eclipse.jetty.http3.qpack.QpackException;
/**
@ -26,8 +26,8 @@ import org.eclipse.jetty.http3.qpack.QpackException;
public class DecoderInstructionParser
{
private final Handler _handler;
private final NBitStringParser _stringParser;
private final NBitIntegerParser _integerParser;
private final NBitStringDecoder _stringDecoder;
private final NBitIntegerDecoder _integerDecoder;
private State _state = State.PARSING;
private Operation _operation = Operation.NONE;
@ -66,8 +66,8 @@ public class DecoderInstructionParser
public DecoderInstructionParser(Handler handler)
{
_handler = handler;
_stringParser = new NBitStringParser();
_integerParser = new NBitIntegerParser();
_stringDecoder = new NBitStringDecoder();
_integerDecoder = new NBitIntegerDecoder();
}
public void parse(ByteBuffer buffer) throws QpackException, EncodingException
@ -92,13 +92,13 @@ public class DecoderInstructionParser
else if ((firstByte & 0x20) != 0)
{
_state = State.SET_CAPACITY;
_integerParser.setPrefix(5);
_integerDecoder.setPrefix(5);
parseSetDynamicTableCapacity(buffer);
}
else
{
_state = State.DUPLICATE;
_integerParser.setPrefix(5);
_integerDecoder.setPrefix(5);
parseDuplicate(buffer);
}
break;
@ -134,20 +134,20 @@ public class DecoderInstructionParser
byte firstByte = buffer.get(buffer.position());
_referenceDynamicTable = (firstByte & 0x40) == 0;
_operation = Operation.INDEX;
_integerParser.setPrefix(6);
_integerDecoder.setPrefix(6);
continue;
case INDEX:
_index = _integerParser.decodeInt(buffer);
_index = _integerDecoder.decodeInt(buffer);
if (_index < 0)
return;
_operation = Operation.VALUE;
_stringParser.setPrefix(8);
_stringDecoder.setPrefix(8);
continue;
case VALUE:
String value = _stringParser.decode(buffer);
String value = _stringDecoder.decode(buffer);
if (value == null)
return;
@ -171,20 +171,20 @@ public class DecoderInstructionParser
{
case NONE:
_operation = Operation.NAME;
_stringParser.setPrefix(6);
_stringDecoder.setPrefix(6);
continue;
case NAME:
_name = _stringParser.decode(buffer);
_name = _stringDecoder.decode(buffer);
if (_name == null)
return;
_operation = Operation.VALUE;
_stringParser.setPrefix(8);
_stringDecoder.setPrefix(8);
continue;
case VALUE:
String value = _stringParser.decode(buffer);
String value = _stringDecoder.decode(buffer);
if (value == null)
return;
@ -201,7 +201,7 @@ public class DecoderInstructionParser
private void parseDuplicate(ByteBuffer buffer) throws QpackException
{
int index = _integerParser.decodeInt(buffer);
int index = _integerDecoder.decodeInt(buffer);
if (index >= 0)
{
reset();
@ -211,7 +211,7 @@ public class DecoderInstructionParser
private void parseSetDynamicTableCapacity(ByteBuffer buffer) throws QpackException
{
int capacity = _integerParser.decodeInt(buffer);
int capacity = _integerDecoder.decodeInt(buffer);
if (capacity >= 0)
{
reset();
@ -221,8 +221,8 @@ public class DecoderInstructionParser
public void reset()
{
_stringParser.reset();
_integerParser.reset();
_stringDecoder.reset();
_integerDecoder.reset();
_state = State.PARSING;
_operation = Operation.NONE;
_referenceDynamicTable = false;

View File

@ -20,8 +20,8 @@ import java.util.List;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http.compression.EncodingException;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.http.compression.NBitStringParser;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.http.compression.NBitStringDecoder;
import org.eclipse.jetty.http3.qpack.QpackDecoder;
import org.eclipse.jetty.http3.qpack.QpackException;
import org.eclipse.jetty.http3.qpack.internal.QpackContext;
@ -36,8 +36,8 @@ public class EncodedFieldSection
{
private static final Logger LOG = LoggerFactory.getLogger(EncodedFieldSection.class);
private final NBitIntegerParser _integerParser = new NBitIntegerParser();
private final NBitStringParser _stringParser = new NBitStringParser();
private final NBitIntegerDecoder _integerDecoder = new NBitIntegerDecoder();
private final NBitStringDecoder _stringDecoder = new NBitStringDecoder();
private final List<EncodedField> _encodedFields = new ArrayList<>();
private final long _streamId;
@ -111,8 +111,8 @@ public class EncodedFieldSection
{
byte firstByte = buffer.get(buffer.position());
boolean dynamicTable = (firstByte & 0x40) == 0;
_integerParser.setPrefix(6);
int index = _integerParser.decodeInt(buffer);
_integerDecoder.setPrefix(6);
int index = _integerDecoder.decodeInt(buffer);
if (index < 0)
throw new EncodingException("invalid_index");
return new IndexedField(dynamicTable, index);
@ -120,8 +120,8 @@ public class EncodedFieldSection
private EncodedField parseIndexedFieldPostBase(ByteBuffer buffer) throws EncodingException
{
_integerParser.setPrefix(4);
int index = _integerParser.decodeInt(buffer);
_integerDecoder.setPrefix(4);
int index = _integerDecoder.decodeInt(buffer);
if (index < 0)
throw new EncodingException("Invalid Index");
@ -137,13 +137,13 @@ public class EncodedFieldSection
boolean allowEncoding = (firstByte & 0x20) != 0;
boolean dynamicTable = (firstByte & 0x10) == 0;
_integerParser.setPrefix(4);
int nameIndex = _integerParser.decodeInt(buffer);
_integerDecoder.setPrefix(4);
int nameIndex = _integerDecoder.decodeInt(buffer);
if (nameIndex < 0)
throw new EncodingException("invalid_name_index");
_stringParser.setPrefix(8);
String value = _stringParser.decode(buffer);
_stringDecoder.setPrefix(8);
String value = _stringDecoder.decode(buffer);
if (value == null)
throw new EncodingException("incomplete_value");
@ -155,13 +155,13 @@ public class EncodedFieldSection
byte firstByte = buffer.get(buffer.position());
boolean allowEncoding = (firstByte & 0x08) != 0;
_integerParser.setPrefix(3);
int nameIndex = _integerParser.decodeInt(buffer);
_integerDecoder.setPrefix(3);
int nameIndex = _integerDecoder.decodeInt(buffer);
if (nameIndex < 0)
throw new EncodingException("invalid_index");
_stringParser.setPrefix(8);
String value = _stringParser.decode(buffer);
_stringDecoder.setPrefix(8);
String value = _stringDecoder.decode(buffer);
if (value == null)
throw new EncodingException("invalid_value");
@ -173,13 +173,13 @@ public class EncodedFieldSection
byte firstByte = buffer.get(buffer.position());
boolean allowEncoding = (firstByte & 0x10) != 0;
_stringParser.setPrefix(4);
String name = _stringParser.decode(buffer);
_stringDecoder.setPrefix(4);
String name = _stringDecoder.decode(buffer);
if (name == null)
throw new EncodingException("invalid_name");
_stringParser.setPrefix(8);
String value = _stringParser.decode(buffer);
_stringDecoder.setPrefix(8);
String value = _stringDecoder.decode(buffer);
if (value == null)
throw new EncodingException("invalid_value");

View File

@ -15,7 +15,7 @@ package org.eclipse.jetty.http3.qpack.internal.parser;
import java.nio.ByteBuffer;
import org.eclipse.jetty.http.compression.NBitIntegerParser;
import org.eclipse.jetty.http.compression.NBitIntegerDecoder;
import org.eclipse.jetty.http3.qpack.QpackException;
/**
@ -28,7 +28,7 @@ public class EncoderInstructionParser
private static final int INSERT_COUNT_INCREMENT_PREFIX = 6;
private final Handler _handler;
private final NBitIntegerParser _integerParser;
private final NBitIntegerDecoder _integerDecoder;
private State _state = State.IDLE;
private enum State
@ -51,7 +51,7 @@ public class EncoderInstructionParser
public EncoderInstructionParser(Handler handler)
{
_handler = handler;
_integerParser = new NBitIntegerParser();
_integerDecoder = new NBitIntegerDecoder();
}
public void parse(ByteBuffer buffer) throws QpackException
@ -67,19 +67,19 @@ public class EncoderInstructionParser
if ((firstByte & 0x80) != 0)
{
_state = State.SECTION_ACKNOWLEDGEMENT;
_integerParser.setPrefix(SECTION_ACKNOWLEDGEMENT_PREFIX);
_integerDecoder.setPrefix(SECTION_ACKNOWLEDGEMENT_PREFIX);
parseSectionAcknowledgment(buffer);
}
else if ((firstByte & 0x40) != 0)
{
_state = State.STREAM_CANCELLATION;
_integerParser.setPrefix(STREAM_CANCELLATION_PREFIX);
_integerDecoder.setPrefix(STREAM_CANCELLATION_PREFIX);
parseStreamCancellation(buffer);
}
else
{
_state = State.INSERT_COUNT_INCREMENT;
_integerParser.setPrefix(INSERT_COUNT_INCREMENT_PREFIX);
_integerDecoder.setPrefix(INSERT_COUNT_INCREMENT_PREFIX);
parseInsertCountIncrement(buffer);
}
break;
@ -103,7 +103,7 @@ public class EncoderInstructionParser
private void parseSectionAcknowledgment(ByteBuffer buffer) throws QpackException
{
long streamId = _integerParser.decodeInt(buffer);
long streamId = _integerDecoder.decodeInt(buffer);
if (streamId >= 0)
{
reset();
@ -113,7 +113,7 @@ public class EncoderInstructionParser
private void parseStreamCancellation(ByteBuffer buffer) throws QpackException
{
long streamId = _integerParser.decodeLong(buffer);
long streamId = _integerDecoder.decodeLong(buffer);
if (streamId >= 0)
{
reset();
@ -123,7 +123,7 @@ public class EncoderInstructionParser
private void parseInsertCountIncrement(ByteBuffer buffer) throws QpackException
{
int increment = _integerParser.decodeInt(buffer);
int increment = _integerDecoder.decodeInt(buffer);
if (increment >= 0)
{
reset();
@ -134,6 +134,6 @@ public class EncoderInstructionParser
public void reset()
{
_state = State.IDLE;
_integerParser.reset();
_integerDecoder.reset();
}
}