Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TestLittleEndianStreams.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 static org.junit.jupiter.api.Assertions.assertArrayEquals;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertFalse;
  19. import static org.junit.jupiter.api.Assertions.assertThrows;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import java.io.IOException;
  22. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  23. import org.junit.jupiter.api.Test;
  24. /**
  25. * Class to test {@link LittleEndianInputStream} and {@link LittleEndianOutputStream}
  26. */
  27. final class TestLittleEndianStreams {
  28. @Test
  29. void testRead() throws IOException {
  30. UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
  31. try (LittleEndianOutputStream leo = new LittleEndianOutputStream(baos)) {
  32. leo.writeInt(12345678);
  33. leo.writeShort(12345);
  34. leo.writeByte(123);
  35. leo.writeShort(40000);
  36. leo.writeByte(200);
  37. leo.writeLong(1234567890123456789L);
  38. leo.writeDouble(123.456);
  39. }
  40. try (LittleEndianInputStream lei = new LittleEndianInputStream(baos.toInputStream())) {
  41. assertEquals(12345678, lei.readInt());
  42. assertEquals(12345, lei.readShort());
  43. assertEquals(123, lei.readByte());
  44. assertEquals(40000, lei.readUShort());
  45. assertEquals(200, lei.readUByte());
  46. assertEquals(1234567890123456789L, lei.readLong());
  47. assertEquals(123.456, lei.readDouble(), 0.0);
  48. }
  49. }
  50. /**
  51. * Up until svn r836101 {@link LittleEndianByteArrayInputStream#readFully(byte[], int, int)}
  52. * had an error which resulted in the data being read and written back to the source byte
  53. * array.
  54. */
  55. @Test
  56. void testReadFully() {
  57. byte[] srcBuf = HexRead.readFromString("99 88 77 66 55 44 33");
  58. LittleEndianInput lei = new LittleEndianByteArrayInputStream(srcBuf);
  59. // do initial read to increment the read index beyond zero
  60. assertEquals(0x8899, lei.readUShort());
  61. byte[] actBuf = new byte[4];
  62. lei.readFully(actBuf);
  63. assertFalse(actBuf[0] == 0x00 && srcBuf[0] == 0x77 && srcBuf[3] == 0x44,
  64. "Identified bug in readFully() - source buffer was modified");
  65. byte[] expBuf = HexRead.readFromString("77 66 55 44");
  66. assertArrayEquals(actBuf, expBuf);
  67. assertEquals(0x33, lei.readUByte());
  68. assertEquals(0, lei.available());
  69. }
  70. @Test
  71. void testBufferOverrun() {
  72. byte[] srcBuf = HexRead.readFromString("99 88 77");
  73. LittleEndianInput lei = new LittleEndianByteArrayInputStream(srcBuf);
  74. // do initial read to increment the read index beyond zero
  75. assertEquals(0x8899, lei.readUShort());
  76. // only one byte left, so this should fail
  77. RuntimeException ex = assertThrows(RuntimeException.class, () -> lei.readFully(new byte[4]));
  78. assertTrue(ex.getMessage().contains("Buffer overrun"));
  79. }
  80. @Test
  81. void testBufferOverrunStartOffset() {
  82. byte[] srcBuf = HexRead.readFromString("99 88 77 88 99");
  83. LittleEndianInput lei = new LittleEndianByteArrayInputStream(srcBuf, 2);
  84. // only one byte left, so this should fail
  85. RuntimeException ex = assertThrows(RuntimeException.class, () -> lei.readFully(new byte[4]));
  86. assertTrue(ex.getMessage().contains("Buffer overrun"));
  87. }
  88. @Test
  89. void testBufferOverrunStartOffset2() {
  90. byte[] srcBuf = HexRead.readFromString("99 88 77 88 99");
  91. LittleEndianInput lei = new LittleEndianByteArrayInputStream(srcBuf, 2, 2);
  92. // only one byte left, so this should fail
  93. RuntimeException ex = assertThrows(RuntimeException.class, () -> lei.readFully(new byte[4]));
  94. assertTrue(ex.getMessage().contains("Buffer overrun"));
  95. }
  96. }