@@ -952,20 +952,20 @@ public int toByteIndex(int cIndex) { |
952 | 952 | @Override |
953 | 953 | public byte getByte(int index) { |
954 | 954 | Component c = findComponent(index); |
955 | | - return c.buf.getByte(c.idx(index)); |
| 955 | + return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index)); |
956 | 956 | } |
957 | 957 |
|
958 | 958 | @Override |
959 | 959 | protected byte _getByte(int index) { |
960 | 960 | Component c = findComponent0(index); |
961 | | - return c.buf.getByte(c.idx(index)); |
| 961 | + return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index)); |
962 | 962 | } |
963 | 963 |
|
964 | 964 | @Override |
965 | 965 | protected short _getShort(int index) { |
966 | 966 | Component c = findComponent0(index); |
967 | 967 | if (index + 2 <= c.endOffset) { |
968 | | - return c.buf.getShort(c.idx(index)); |
| 968 | + return c.abuf != null ? c.abuf._getShort(c.idx(index)) : c.buf.getShort(c.idx(index)); |
969 | 969 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
970 | 970 | return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff); |
971 | 971 | } else { |
@@ -977,7 +977,7 @@ protected short _getShort(int index) { |
977 | 977 | protected short _getShortLE(int index) { |
978 | 978 | Component c = findComponent0(index); |
979 | 979 | if (index + 2 <= c.endOffset) { |
980 | | - return c.buf.getShortLE(c.idx(index)); |
| 980 | + return c.abuf != null ? c.abuf._getShortLE(c.idx(index)) : c.buf.getShortLE(c.idx(index)); |
981 | 981 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
982 | 982 | return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8); |
983 | 983 | } else { |
@@ -989,7 +989,8 @@ protected short _getShortLE(int index) { |
989 | 989 | protected int _getUnsignedMedium(int index) { |
990 | 990 | Component c = findComponent0(index); |
991 | 991 | if (index + 3 <= c.endOffset) { |
992 | | - return c.buf.getUnsignedMedium(c.idx(index)); |
| 992 | + return c.abuf != null ? c.abuf._getUnsignedMedium(c.idx(index)) |
| 993 | + : c.buf.getUnsignedMedium(c.idx(index)); |
993 | 994 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
994 | 995 | return (_getShort(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff; |
995 | 996 | } else { |
@@ -1001,7 +1002,8 @@ protected int _getUnsignedMedium(int index) { |
1001 | 1002 | protected int _getUnsignedMediumLE(int index) { |
1002 | 1003 | Component c = findComponent0(index); |
1003 | 1004 | if (index + 3 <= c.endOffset) { |
1004 | | - return c.buf.getUnsignedMediumLE(c.idx(index)); |
| 1005 | + return c.abuf != null ? c.abuf._getUnsignedMediumLE(c.idx(index)) |
| 1006 | + : c.buf.getUnsignedMediumLE(c.idx(index)); |
1005 | 1007 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1006 | 1008 | return _getShortLE(index) & 0xffff | (_getByte(index + 2) & 0xff) << 16; |
1007 | 1009 | } else { |
@@ -1013,7 +1015,7 @@ protected int _getUnsignedMediumLE(int index) { |
1013 | 1015 | protected int _getInt(int index) { |
1014 | 1016 | Component c = findComponent0(index); |
1015 | 1017 | if (index + 4 <= c.endOffset) { |
1016 | | - return c.buf.getInt(c.idx(index)); |
| 1018 | + return c.abuf != null ? c.abuf._getInt(c.idx(index)) : c.buf.getInt(c.idx(index)); |
1017 | 1019 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1018 | 1020 | return (_getShort(index) & 0xffff) << 16 | _getShort(index + 2) & 0xffff; |
1019 | 1021 | } else { |
@@ -1025,7 +1027,7 @@ protected int _getInt(int index) { |
1025 | 1027 | protected int _getIntLE(int index) { |
1026 | 1028 | Component c = findComponent0(index); |
1027 | 1029 | if (index + 4 <= c.endOffset) { |
1028 | | - return c.buf.getIntLE(c.idx(index)); |
| 1030 | + return c.abuf != null ? c.abuf._getIntLE(c.idx(index)) : c.buf.getIntLE(c.idx(index)); |
1029 | 1031 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1030 | 1032 | return _getShortLE(index) & 0xffff | (_getShortLE(index + 2) & 0xffff) << 16; |
1031 | 1033 | } else { |
@@ -1037,7 +1039,7 @@ protected int _getIntLE(int index) { |
1037 | 1039 | protected long _getLong(int index) { |
1038 | 1040 | Component c = findComponent0(index); |
1039 | 1041 | if (index + 8 <= c.endOffset) { |
1040 | | - return c.buf.getLong(c.idx(index)); |
| 1042 | + return c.abuf != null ? c.abuf._getLong(c.idx(index)) : c.buf.getLong(c.idx(index)); |
1041 | 1043 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1042 | 1044 | return (_getInt(index) & 0xffffffffL) << 32 | _getInt(index + 4) & 0xffffffffL; |
1043 | 1045 | } else { |
@@ -1049,7 +1051,7 @@ protected long _getLong(int index) { |
1049 | 1051 | protected long _getLongLE(int index) { |
1050 | 1052 | Component c = findComponent0(index); |
1051 | 1053 | if (index + 8 <= c.endOffset) { |
1052 | | - return c.buf.getLongLE(c.idx(index)); |
| 1054 | + return c.abuf != null ? c.abuf._getLongLE(c.idx(index)) : c.buf.getLongLE(c.idx(index)); |
1053 | 1055 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1054 | 1056 | return _getIntLE(index) & 0xffffffffL | (_getIntLE(index + 4) & 0xffffffffL) << 32; |
1055 | 1057 | } else { |
@@ -1180,14 +1182,22 @@ public CompositeByteBuf getBytes(int index, OutputStream out, int length) throws |
1180 | 1182 | @Override |
1181 | 1183 | public CompositeByteBuf setByte(int index, int value) { |
1182 | 1184 | Component c = findComponent(index); |
1183 | | - c.buf.setByte(c.idx(index), value); |
| 1185 | + if (c.abuf != null) { |
| 1186 | + c.abuf._setByte(c.idx(index), value); |
| 1187 | + } else { |
| 1188 | + c.buf.setByte(c.idx(index), value); |
| 1189 | + } |
1184 | 1190 | return this; |
1185 | 1191 | } |
1186 | 1192 |
|
1187 | 1193 | @Override |
1188 | 1194 | protected void _setByte(int index, int value) { |
1189 | 1195 | Component c = findComponent0(index); |
1190 | | - c.buf.setByte(c.idx(index), value); |
| 1196 | + if (c.abuf != null) { |
| 1197 | + c.abuf._setByte(c.idx(index), value); |
| 1198 | + } else { |
| 1199 | + c.buf.setByte(c.idx(index), value); |
| 1200 | + } |
1191 | 1201 | } |
1192 | 1202 |
|
1193 | 1203 | @Override |
@@ -1201,7 +1211,11 @@ public CompositeByteBuf setShort(int index, int value) { |
1201 | 1211 | protected void _setShort(int index, int value) { |
1202 | 1212 | Component c = findComponent0(index); |
1203 | 1213 | if (index + 2 <= c.endOffset) { |
1204 | | - c.buf.setShort(c.idx(index), value); |
| 1214 | + if (c.abuf != null) { |
| 1215 | + c.abuf._setShort(c.idx(index), value); |
| 1216 | + } else { |
| 1217 | + c.buf.setShort(c.idx(index), value); |
| 1218 | + } |
1205 | 1219 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1206 | 1220 | _setByte(index, (byte) (value >>> 8)); |
1207 | 1221 | _setByte(index + 1, (byte) value); |
@@ -1215,7 +1229,11 @@ protected void _setShort(int index, int value) { |
1215 | 1229 | protected void _setShortLE(int index, int value) { |
1216 | 1230 | Component c = findComponent0(index); |
1217 | 1231 | if (index + 2 <= c.endOffset) { |
1218 | | - c.buf.setShortLE(c.idx(index), value); |
| 1232 | + if (c.abuf != null) { |
| 1233 | + c.abuf._setShortLE(c.idx(index), value); |
| 1234 | + } else { |
| 1235 | + c.buf.setShortLE(c.idx(index), value); |
| 1236 | + } |
1219 | 1237 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1220 | 1238 | _setByte(index, (byte) value); |
1221 | 1239 | _setByte(index + 1, (byte) (value >>> 8)); |
@@ -1236,7 +1254,11 @@ public CompositeByteBuf setMedium(int index, int value) { |
1236 | 1254 | protected void _setMedium(int index, int value) { |
1237 | 1255 | Component c = findComponent0(index); |
1238 | 1256 | if (index + 3 <= c.endOffset) { |
1239 | | - c.buf.setMedium(c.idx(index), value); |
| 1257 | + if (c.abuf != null) { |
| 1258 | + c.abuf._setMedium(c.idx(index), value); |
| 1259 | + } else { |
| 1260 | + c.buf.setMedium(c.idx(index), value); |
| 1261 | + } |
1240 | 1262 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1241 | 1263 | _setShort(index, (short) (value >> 8)); |
1242 | 1264 | _setByte(index + 2, (byte) value); |
@@ -1250,7 +1272,11 @@ protected void _setMedium(int index, int value) { |
1250 | 1272 | protected void _setMediumLE(int index, int value) { |
1251 | 1273 | Component c = findComponent0(index); |
1252 | 1274 | if (index + 3 <= c.endOffset) { |
1253 | | - c.buf.setMediumLE(c.idx(index), value); |
| 1275 | + if (c.abuf != null) { |
| 1276 | + c.abuf._setMediumLE(c.idx(index), value); |
| 1277 | + } else { |
| 1278 | + c.buf.setMediumLE(c.idx(index), value); |
| 1279 | + } |
1254 | 1280 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1255 | 1281 | _setShortLE(index, (short) value); |
1256 | 1282 | _setByte(index + 2, (byte) (value >>> 16)); |
@@ -1271,7 +1297,11 @@ public CompositeByteBuf setInt(int index, int value) { |
1271 | 1297 | protected void _setInt(int index, int value) { |
1272 | 1298 | Component c = findComponent0(index); |
1273 | 1299 | if (index + 4 <= c.endOffset) { |
1274 | | - c.buf.setInt(c.idx(index), value); |
| 1300 | + if (c.abuf != null) { |
| 1301 | + c.abuf._setInt(c.idx(index), value); |
| 1302 | + } else { |
| 1303 | + c.buf.setInt(c.idx(index), value); |
| 1304 | + } |
1275 | 1305 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1276 | 1306 | _setShort(index, (short) (value >>> 16)); |
1277 | 1307 | _setShort(index + 2, (short) value); |
@@ -1285,7 +1315,11 @@ protected void _setInt(int index, int value) { |
1285 | 1315 | protected void _setIntLE(int index, int value) { |
1286 | 1316 | Component c = findComponent0(index); |
1287 | 1317 | if (index + 4 <= c.endOffset) { |
1288 | | - c.buf.setIntLE(c.idx(index), value); |
| 1318 | + if (c.abuf != null) { |
| 1319 | + c.abuf._setIntLE(c.idx(index), value); |
| 1320 | + } else { |
| 1321 | + c.buf.setIntLE(c.idx(index), value); |
| 1322 | + } |
1289 | 1323 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1290 | 1324 | _setShortLE(index, (short) value); |
1291 | 1325 | _setShortLE(index + 2, (short) (value >>> 16)); |
@@ -1306,7 +1340,11 @@ public CompositeByteBuf setLong(int index, long value) { |
1306 | 1340 | protected void _setLong(int index, long value) { |
1307 | 1341 | Component c = findComponent0(index); |
1308 | 1342 | if (index + 8 <= c.endOffset) { |
1309 | | - c.buf.setLong(c.idx(index), value); |
| 1343 | + if (c.abuf != null) { |
| 1344 | + c.abuf._setLong(c.idx(index), value); |
| 1345 | + } else { |
| 1346 | + c.buf.setLong(c.idx(index), value); |
| 1347 | + } |
1310 | 1348 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1311 | 1349 | _setInt(index, (int) (value >>> 32)); |
1312 | 1350 | _setInt(index + 4, (int) value); |
@@ -1320,7 +1358,11 @@ protected void _setLong(int index, long value) { |
1320 | 1358 | protected void _setLongLE(int index, long value) { |
1321 | 1359 | Component c = findComponent0(index); |
1322 | 1360 | if (index + 8 <= c.endOffset) { |
1323 | | - c.buf.setLongLE(c.idx(index), value); |
| 1361 | + if (c.abuf != null) { |
| 1362 | + c.abuf._setLongLE(c.idx(index), value); |
| 1363 | + } else { |
| 1364 | + c.buf.setLongLE(c.idx(index), value); |
| 1365 | + } |
1324 | 1366 | } else if (order() == ByteOrder.BIG_ENDIAN) { |
1325 | 1367 | _setIntLE(index, (int) value); |
1326 | 1368 | _setIntLE(index + 4, (int) (value >>> 32)); |
@@ -1913,6 +1955,7 @@ public String toString() { |
1913 | 1955 | private static final class Component { |
1914 | 1956 | final ByteBuf srcBuf; // the originally added buffer |
1915 | 1957 | final ByteBuf buf; // srcBuf unwrapped zero or more times |
| 1958 | + final AbstractByteBuf abuf; // buf cast to AbstractByteBuf, or null if not an instance |
1916 | 1959 |
|
1917 | 1960 | int srcAdjustment; // index of the start of this CompositeByteBuf relative to srcBuf |
1918 | 1961 | int adjustment; // index of the start of this CompositeByteBuf relative to buf |
@@ -1927,6 +1970,7 @@ private static final class Component { |
1927 | 1970 | this.srcBuf = srcBuf; |
1928 | 1971 | this.srcAdjustment = srcOffset - offset; |
1929 | 1972 | this.buf = buf; |
| 1973 | + this.abuf = buf instanceof AbstractByteBuf ? (AbstractByteBuf) buf : null; |
1930 | 1974 | this.adjustment = bufOffset - offset; |
1931 | 1975 | this.offset = offset; |
1932 | 1976 | this.endOffset = offset + len; |
|