Skip to content

Commit

Permalink
ByteString arrayOffset method
Browse files Browse the repository at this point in the history
Motivation:
The ByteString class currently assumes the underlying array will be a complete representation of data. This is limiting as it does not allow a subsection of another array to be used. The forces copy operations to take place to compensate for the lack of API support.

Modifications:
- add arrayOffset method to ByteString
- modify all ByteString and AsciiString methods that loop over or index into the underlying array to use this offset
- update all code that uses ByteString.array to ensure it accounts for the offset
- add unit tests to test the implementation respects the offset

Result:
ByteString and AsciiString can represent a sub region of a byte[].
  • Loading branch information
Scottmitch committed Apr 25, 2015
1 parent c981957 commit f1e122a
Show file tree
Hide file tree
Showing 10 changed files with 582 additions and 316 deletions.
4 changes: 2 additions & 2 deletions buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public static void copy(ByteString src, int srcIdx, ByteBuf dst, int dstIdx, int
+ length + ") <= srcLen(" + thisLen + ')');
}

checkNotNull(dst, "dst").setBytes(dstIdx, src.array(), srcIdx, length);
checkNotNull(dst, "dst").setBytes(dstIdx, src.array(), srcIdx + src.arrayOffset(), length);
}

/**
Expand All @@ -628,7 +628,7 @@ public static void copy(ByteString src, int srcIdx, ByteBuf dst, int length) {
+ length + ") <= srcLen(" + thisLen + ')');
}

checkNotNull(dst, "dst").writeBytes(src.array(), srcIdx, length);
checkNotNull(dst, "dst").writeBytes(src.array(), srcIdx + src.arrayOffset(), length);
}

static final class ThreadLocalUnsafeDirectByteBuf extends UnpooledUnsafeDirectByteBuf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ public Configuration configuration() {
}

private void encodeHeader(ByteString key, ByteString value, OutputStream stream) throws IOException {
encoder.encodeHeader(stream, key.array(), value.array(), sensitivityDetector.isSensitive(key, value));
encoder.encodeHeader(stream,
key.isEntireArrayUsed() ? key.array() : new ByteString(key, true).array(),
value.isEntireArrayUsed() ? value.array() : new ByteString(value, true).array(),
sensitivityDetector.isSensitive(key, value));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public boolean visit(Entry<ByteString, ByteString> entry) throws Http2Exception
throw streamError(streamId, PROTOCOL_ERROR,
"Invalid HTTP/2 header '%s' encountered in translation to HTTP/1.x", translatedName);
} else {
output.add(new AsciiString(translatedName.array(), false), new AsciiString(value.array(), false));
output.add(new AsciiString(translatedName, false), new AsciiString(value, false));
}
}
return true;
Expand Down
Loading

0 comments on commit f1e122a

Please sign in to comment.