I think the layout of tags is missing now in the javadoc. May be it got
missed or moved to some other place?
I remember we had a layout explaining the tag structure then this code is
much easier to read this code.
As Chia-Ping said <tag len(short)><tag type (byte)<tag bytes> is the
layout.
So from the KeyValue lay out we extract the tag part which in itself has a
tag length to represent the complete set of tags.
>From the tags offset and tags length from the KV we extract individual tags
in that KV.
For eg
See TagUtil#asList
{code}
List<Tag> tags = new ArrayList<>();
int pos = offset;
while (pos < offset + length) {
int tagLen = Bytes.readAsInt(b, pos, TAG_LENGTH_SIZE);
tags.add(new ArrayBackedTag(b, pos, tagLen + TAG_LENGTH_SIZE));
pos += TAG_LENGTH_SIZE + tagLen;
}
return tags;
{code}
Regards
Ram
On Mon, Aug 7, 2017 at 3:25 AM, Ted Yu <[email protected]> wrote:
> The byte following the tag length (a short) is the tag type.
>
> The current code is correct.
>
> On Sun, Aug 6, 2017 at 5:40 AM, Chia-Ping Tsai <[email protected]>
> wrote:
>
> > According to the following code:
> > public ArrayBackedTag(byte tagType, byte[] tag) {
> > int tagLength = tag.length + TYPE_LENGTH_SIZE;
> > if (tagLength > MAX_TAG_LENGTH) {
> > throw new IllegalArgumentException(
> > "Invalid tag data being passed. Its length can not exceed " +
> > MAX_TAG_LENGTH);
> > }
> > length = TAG_LENGTH_SIZE + tagLength;
> > bytes = new byte[length];
> > int pos = Bytes.putAsShort(bytes, 0, tagLength);
> > pos = Bytes.putByte(bytes, pos, tagType);
> > Bytes.putBytes(bytes, pos, tag, 0, tag.length);
> > this.type = tagType;
> > }
> > The layout of the byte array should be:
> > |tag legnth (2 bytes)|tag type(1 byte)|tag|
> >
> > It seems to me that the "bytes[offset + TYPE_LENGTH_SIZE]" is correct.
> >
> > On 2017-08-06 16:35, Lars George <[email protected]> wrote:
> > > Hi,
> > >
> > > I found this reading through tags in 1.3, but checked in trunk as
> > > well. There is this code:
> > >
> > > public ArrayBackedTag(byte[] bytes, int offset, int length) {
> > > if (length > MAX_TAG_LENGTH) {
> > > throw new IllegalArgumentException(
> > > "Invalid tag data being passed. Its length can not exceed "
> > > + MAX_TAG_LENGTH);
> > > }
> > > this.bytes = bytes;
> > > this.offset = offset;
> > > this.length = length;
> > > this.type = bytes[offset + TAG_LENGTH_SIZE];
> > > }
> > >
> > > I am concerned about the last line of the code, using the wrong
> constant?
> > >
> > > public final static int TYPE_LENGTH_SIZE = Bytes.SIZEOF_BYTE;
> > > public final static int TAG_LENGTH_SIZE = Bytes.SIZEOF_SHORT;
> > >
> > > Should this not read
> > >
> > > this.type = bytes[offset + TYPE_LENGTH_SIZE];
> > >
> > > Would this not read the type from the wrong place in the array?
> > >
> > > Cheers,
> > > Lars
> > >
> >
>