You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LittleEndianByteArrayOutputStream.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.OutputStream;
  17. /**
  18. * Adapts a plain byte array to {@link LittleEndianOutput}
  19. */
  20. public final class LittleEndianByteArrayOutputStream extends OutputStream implements LittleEndianOutput, DelayableLittleEndianOutput {
  21. private final byte[] _buf;
  22. private final int _endIndex;
  23. private int _writeIndex;
  24. public LittleEndianByteArrayOutputStream(byte[] buf, int startOffset, int maxWriteLen) { // NOSONAR
  25. if (startOffset < 0 || startOffset > buf.length) {
  26. throw new IllegalArgumentException("Specified startOffset (" + startOffset
  27. + ") is out of allowable range (0.." + buf.length + ")");
  28. }
  29. _buf = buf;
  30. _writeIndex = startOffset;
  31. _endIndex = startOffset + maxWriteLen;
  32. if (_endIndex < startOffset || _endIndex > buf.length) {
  33. throw new IllegalArgumentException("calculated end index (" + _endIndex
  34. + ") is out of allowable range (" + _writeIndex + ".." + buf.length + ")");
  35. }
  36. }
  37. public LittleEndianByteArrayOutputStream(byte[] buf, int startOffset) {
  38. this(buf, startOffset, buf.length - startOffset);
  39. }
  40. private void checkPosition(int i) {
  41. if (i > _endIndex - _writeIndex) {
  42. throw new RuntimeException("Buffer overrun");
  43. }
  44. }
  45. @Override
  46. public void writeByte(int v) {
  47. checkPosition(1);
  48. _buf[_writeIndex++] = (byte)v;
  49. }
  50. @Override
  51. public void writeDouble(double v) {
  52. writeLong(Double.doubleToLongBits(v));
  53. }
  54. @Override
  55. public void writeInt(int v) {
  56. checkPosition(4);
  57. int i = _writeIndex;
  58. _buf[i++] = (byte)((v >>> 0) & 0xFF);
  59. _buf[i++] = (byte)((v >>> 8) & 0xFF);
  60. _buf[i++] = (byte)((v >>> 16) & 0xFF);
  61. _buf[i++] = (byte)((v >>> 24) & 0xFF);
  62. _writeIndex = i;
  63. }
  64. @Override
  65. public void writeLong(long v) {
  66. writeInt((int)(v >> 0));
  67. writeInt((int)(v >> 32));
  68. }
  69. @Override
  70. public void writeShort(int v) {
  71. checkPosition(2);
  72. int i = _writeIndex;
  73. _buf[i++] = (byte)((v >>> 0) & 0xFF);
  74. _buf[i++] = (byte)((v >>> 8) & 0xFF);
  75. _writeIndex = i;
  76. }
  77. @Override
  78. public void write(int b) {
  79. writeByte(b);
  80. }
  81. @Override
  82. public void write(byte[] b) {
  83. int len = b.length;
  84. checkPosition(len);
  85. System.arraycopy(b, 0, _buf, _writeIndex, len);
  86. _writeIndex += len;
  87. }
  88. @Override
  89. public void write(byte[] b, int offset, int len) {
  90. checkPosition(len);
  91. System.arraycopy(b, offset, _buf, _writeIndex, len);
  92. _writeIndex += len;
  93. }
  94. public int getWriteIndex() {
  95. return _writeIndex;
  96. }
  97. @Override
  98. public LittleEndianOutput createDelayedOutput(int size) {
  99. checkPosition(size);
  100. LittleEndianOutput result = new LittleEndianByteArrayOutputStream(_buf, _writeIndex, size);
  101. _writeIndex += size;
  102. return result;
  103. }
  104. }