1 /* 2 * Copyright 2016 The Netty Project 3 * 4 * The Netty Project licenses this file to you under the Apache License, 5 * version 2.0 (the "License"); you may not use this file except in compliance 6 * with the License. You may obtain a copy of the License at: 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" ~BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations 14 * under the License. 15 */ 16 module hunt.net.buffer.AbstractUnpooledSlicedByteBuf; 17 18 import hunt.net.buffer.AbstractByteBuf; 19 import hunt.net.buffer.ByteBuf; 20 import hunt.net.buffer.ByteBufAllocator; 21 import hunt.net.buffer.ByteBufUtil; 22 import hunt.net.buffer.ByteProcessor; 23 24 import hunt.Byte; 25 import hunt.io.ByteBuffer; 26 import hunt.Exceptions; 27 import hunt.net.Exceptions; 28 import hunt.stream.Common; 29 import hunt.util.StringBuilder; 30 import hunt.util.ByteOrder; 31 32 import std.conv; 33 import std.format; 34 35 36 // import java.io.IOException; 37 // import java.io.InputStream; 38 // import java.io.OutputStream; 39 // import java.nio.ByteBuffer; 40 // import java.nio.ByteOrder; 41 // import java.nio.channels.FileChannel; 42 // import java.nio.channels.GatheringByteChannel; 43 // import java.nio.channels.ScatteringByteChannel; 44 // import java.nio.charset.Charset; 45 46 // import static io.netty.util.internal.MathUtil.isOutOfBounds; 47 48 abstract class AbstractUnpooledSlicedByteBuf : AbstractByteBuf { 49 private ByteBuf buffer; 50 private int adjustment; 51 52 this(ByteBuf buffer, int index, int length) { 53 super(length); 54 checkSliceOutOfBounds(index, length, buffer); 55 56 AbstractUnpooledSlicedByteBuf slicedBuffer = cast(AbstractUnpooledSlicedByteBuf) buffer; 57 58 if (slicedBuffer !is null) { 59 this.buffer = slicedBuffer.buffer; 60 adjustment = slicedBuffer.adjustment + index; 61 // } else { 62 // if (buffer instanceof DuplicatedByteBuf) { 63 // this.buffer = buffer.unwrap(); 64 // adjustment = index; 65 // } 66 } else { 67 this.buffer = buffer; 68 adjustment = index; 69 } 70 71 initLength(length); 72 writerIndex(length); 73 } 74 75 /** 76 * Called by the constructor before {@link #writerIndex(int)}. 77 * @param length the {@code length} argument from the constructor. 78 */ 79 void initLength(int length) { 80 } 81 82 int length() { 83 return capacity(); 84 } 85 86 override 87 ByteBuf unwrap() { 88 return buffer; 89 } 90 91 override 92 ByteBufAllocator alloc() { 93 return unwrap().alloc(); 94 } 95 96 override 97 ByteOrder order() { 98 return unwrap().order(); 99 } 100 101 override 102 bool isDirect() { 103 return unwrap().isDirect(); 104 } 105 106 override 107 ByteBuf capacity(int newCapacity) { 108 throw new UnsupportedOperationException("sliced buffer"); 109 } 110 111 alias capacity = ByteBuf.capacity; 112 113 override 114 bool hasArray() { 115 return unwrap().hasArray(); 116 } 117 118 override 119 byte[] array() { 120 return unwrap().array(); 121 } 122 123 override 124 int arrayOffset() { 125 return idx(unwrap().arrayOffset()); 126 } 127 128 override 129 bool hasMemoryAddress() { 130 return unwrap().hasMemoryAddress(); 131 } 132 133 override 134 long memoryAddress() { 135 return unwrap().memoryAddress() + adjustment; 136 } 137 138 override 139 byte getByte(int index) { 140 checkIndex0(index, 1); 141 return unwrap().getByte(idx(index)); 142 } 143 144 override 145 protected byte _getByte(int index) { 146 return unwrap().getByte(idx(index)); 147 } 148 149 override 150 short getShort(int index) { 151 checkIndex0(index, 2); 152 return unwrap().getShort(idx(index)); 153 } 154 155 override 156 protected short _getShort(int index) { 157 return unwrap().getShort(idx(index)); 158 } 159 160 override 161 short getShortLE(int index) { 162 checkIndex0(index, 2); 163 return unwrap().getShortLE(idx(index)); 164 } 165 166 override 167 protected short _getShortLE(int index) { 168 return unwrap().getShortLE(idx(index)); 169 } 170 171 override 172 int getUnsignedMedium(int index) { 173 checkIndex0(index, 3); 174 return unwrap().getUnsignedMedium(idx(index)); 175 } 176 177 override 178 protected int _getUnsignedMedium(int index) { 179 return unwrap().getUnsignedMedium(idx(index)); 180 } 181 182 override 183 int getUnsignedMediumLE(int index) { 184 checkIndex0(index, 3); 185 return unwrap().getUnsignedMediumLE(idx(index)); 186 } 187 188 override 189 protected int _getUnsignedMediumLE(int index) { 190 return unwrap().getUnsignedMediumLE(idx(index)); 191 } 192 193 override 194 int getInt(int index) { 195 checkIndex0(index, 4); 196 return unwrap().getInt(idx(index)); 197 } 198 199 override 200 protected int _getInt(int index) { 201 return unwrap().getInt(idx(index)); 202 } 203 204 override 205 int getIntLE(int index) { 206 checkIndex0(index, 4); 207 return unwrap().getIntLE(idx(index)); 208 } 209 210 override 211 protected int _getIntLE(int index) { 212 return unwrap().getIntLE(idx(index)); 213 } 214 215 override 216 long getLong(int index) { 217 checkIndex0(index, 8); 218 return unwrap().getLong(idx(index)); 219 } 220 221 override 222 protected long _getLong(int index) { 223 return unwrap().getLong(idx(index)); 224 } 225 226 override 227 long getLongLE(int index) { 228 checkIndex0(index, 8); 229 return unwrap().getLongLE(idx(index)); 230 } 231 232 override 233 protected long _getLongLE(int index) { 234 return unwrap().getLongLE(idx(index)); 235 } 236 237 override 238 ByteBuf duplicate() { 239 return unwrap().duplicate().setIndex(idx(readerIndex()), idx(writerIndex())); 240 } 241 242 override 243 ByteBuf copy(int index, int length) { 244 checkIndex0(index, length); 245 return unwrap().copy(idx(index), length); 246 } 247 248 override 249 ByteBuf slice(int index, int length) { 250 checkIndex0(index, length); 251 return unwrap().slice(idx(index), length); 252 } 253 254 override 255 ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { 256 checkIndex0(index, length); 257 unwrap().getBytes(idx(index), dst, dstIndex, length); 258 return this; 259 } 260 261 override 262 ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { 263 checkIndex0(index, length); 264 unwrap().getBytes(idx(index), dst, dstIndex, length); 265 return this; 266 } 267 268 override 269 ByteBuf getBytes(int index, ByteBuffer dst) { 270 checkIndex0(index, dst.remaining()); 271 unwrap().getBytes(idx(index), dst); 272 return this; 273 } 274 275 override 276 ByteBuf setByte(int index, int value) { 277 checkIndex0(index, 1); 278 unwrap().setByte(idx(index), value); 279 return this; 280 } 281 282 // override 283 // CharSequence getCharSequence(int index, int length, Charset charset) { 284 // checkIndex0(index, length); 285 // return unwrap().getCharSequence(idx(index), length, charset); 286 // } 287 288 override 289 protected void _setByte(int index, int value) { 290 unwrap().setByte(idx(index), value); 291 } 292 293 override 294 ByteBuf setShort(int index, int value) { 295 checkIndex0(index, 2); 296 unwrap().setShort(idx(index), value); 297 return this; 298 } 299 300 override 301 protected void _setShort(int index, int value) { 302 unwrap().setShort(idx(index), value); 303 } 304 305 override 306 ByteBuf setShortLE(int index, int value) { 307 checkIndex0(index, 2); 308 unwrap().setShortLE(idx(index), value); 309 return this; 310 } 311 312 override 313 protected void _setShortLE(int index, int value) { 314 unwrap().setShortLE(idx(index), value); 315 } 316 317 override 318 ByteBuf setMedium(int index, int value) { 319 checkIndex0(index, 3); 320 unwrap().setMedium(idx(index), value); 321 return this; 322 } 323 324 override 325 protected void _setMedium(int index, int value) { 326 unwrap().setMedium(idx(index), value); 327 } 328 329 override 330 ByteBuf setMediumLE(int index, int value) { 331 checkIndex0(index, 3); 332 unwrap().setMediumLE(idx(index), value); 333 return this; 334 } 335 336 override 337 protected void _setMediumLE(int index, int value) { 338 unwrap().setMediumLE(idx(index), value); 339 } 340 341 override 342 ByteBuf setInt(int index, int value) { 343 checkIndex0(index, 4); 344 unwrap().setInt(idx(index), value); 345 return this; 346 } 347 348 override 349 protected void _setInt(int index, int value) { 350 unwrap().setInt(idx(index), value); 351 } 352 353 override 354 ByteBuf setIntLE(int index, int value) { 355 checkIndex0(index, 4); 356 unwrap().setIntLE(idx(index), value); 357 return this; 358 } 359 360 override 361 protected void _setIntLE(int index, int value) { 362 unwrap().setIntLE(idx(index), value); 363 } 364 365 override 366 ByteBuf setLong(int index, long value) { 367 checkIndex0(index, 8); 368 unwrap().setLong(idx(index), value); 369 return this; 370 } 371 372 override 373 protected void _setLong(int index, long value) { 374 unwrap().setLong(idx(index), value); 375 } 376 377 override 378 ByteBuf setLongLE(int index, long value) { 379 checkIndex0(index, 8); 380 unwrap().setLongLE(idx(index), value); 381 return this; 382 } 383 384 override 385 protected void _setLongLE(int index, long value) { 386 unwrap().setLongLE(idx(index), value); 387 } 388 389 override 390 ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { 391 checkIndex0(index, length); 392 unwrap().setBytes(idx(index), src, srcIndex, length); 393 return this; 394 } 395 396 override 397 ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { 398 checkIndex0(index, length); 399 unwrap().setBytes(idx(index), src, srcIndex, length); 400 return this; 401 } 402 403 override 404 ByteBuf setBytes(int index, ByteBuffer src) { 405 checkIndex0(index, src.remaining()); 406 unwrap().setBytes(idx(index), src); 407 return this; 408 } 409 410 override 411 ByteBuf getBytes(int index, OutputStream output, int length) { 412 checkIndex0(index, length); 413 unwrap().getBytes(idx(index), output, length); 414 return this; 415 } 416 417 // override 418 // int getBytes(int index, GatheringByteChannel output, int length) { 419 // checkIndex0(index, length); 420 // return unwrap().getBytes(idx(index), output, length); 421 // } 422 423 // override 424 // int getBytes(int index, FileChannel output, long position, int length) { 425 // checkIndex0(index, length); 426 // return unwrap().getBytes(idx(index), output, position, length); 427 // } 428 429 override 430 int setBytes(int index, InputStream input, int length) { 431 checkIndex0(index, length); 432 return unwrap().setBytes(idx(index), input, length); 433 } 434 435 // override 436 // int setBytes(int index, ScatteringByteChannel input, int length) { 437 // checkIndex0(index, length); 438 // return unwrap().setBytes(idx(index), input, length); 439 // } 440 441 // override 442 // int setBytes(int index, FileChannel input, long position, int length) { 443 // checkIndex0(index, length); 444 // return unwrap().setBytes(idx(index), input, position, length); 445 // } 446 447 // override 448 final int refCnt() { 449 return refCnt0(); 450 } 451 452 int refCnt0() { 453 return unwrap().refCnt(); 454 } 455 456 override 457 final ByteBuf retain() { 458 return retain0(); 459 } 460 461 ByteBuf retain0() { 462 unwrap().retain(); 463 return this; 464 } 465 466 override 467 final ByteBuf retain(int increment) { 468 return retain0(increment); 469 } 470 471 ByteBuf retain0(int increment) { 472 unwrap().retain(increment); 473 return this; 474 } 475 476 override 477 final ByteBuf touch() { 478 return touch0(); 479 } 480 481 ByteBuf touch0() { 482 unwrap().touch(); 483 return this; 484 } 485 486 override 487 final ByteBuf touch(Object hint) { 488 return touch0(hint); 489 } 490 491 ByteBuf touch0(Object hint) { 492 unwrap().touch(hint); 493 return this; 494 } 495 496 // override 497 final bool release() { 498 return release0(); 499 } 500 501 bool release0() { 502 return unwrap().release(); 503 } 504 505 // override 506 final bool release(int decrement) { 507 return release0(decrement); 508 } 509 510 bool release0(int decrement) { 511 return unwrap().release(decrement); 512 } 513 514 override 515 int nioBufferCount() { 516 return unwrap().nioBufferCount(); 517 } 518 519 override 520 ByteBuffer internalNioBuffer(int index, int length) { 521 return nioBuffer(index, length); 522 } 523 524 override 525 ByteBuffer nioBuffer(int index, int length) { 526 checkIndex0(index, length); 527 return unwrap().nioBuffer(idx(index), length); 528 } 529 530 override 531 ByteBuffer[] nioBuffers(int index, int length) { 532 checkIndex0(index, length); 533 return unwrap().nioBuffers(idx(index), length); 534 } 535 536 override 537 int forEachByte(int index, int length, ByteProcessor processor) { 538 checkIndex0(index, length); 539 int ret = unwrap().forEachByte(idx(index), length, processor); 540 if (ret >= adjustment) { 541 return ret - adjustment; 542 } else { 543 return -1; 544 } 545 } 546 547 override 548 int forEachByteDesc(int index, int length, ByteProcessor processor) { 549 checkIndex0(index, length); 550 int ret = unwrap().forEachByteDesc(idx(index), length, processor); 551 if (ret >= adjustment) { 552 return ret - adjustment; 553 } else { 554 return -1; 555 } 556 } 557 558 /** 559 * Returns the index with the needed adjustment. 560 */ 561 final int idx(int index) { 562 return index + adjustment; 563 } 564 565 static void checkSliceOutOfBounds(int index, int length, ByteBuf buffer) { 566 if (isOutOfBounds(index, length, buffer.capacity())) { 567 string msg = (cast(Object)buffer).toString() ~ ".slice(" ~ 568 index.to!string() ~ ", " ~ length.to!string() ~ ")"; 569 throw new IndexOutOfBoundsException(msg); 570 } 571 } 572 }