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.

LittleEndianOutputStream.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.FilterOutputStream;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. public final class LittleEndianOutputStream extends FilterOutputStream implements LittleEndianOutput {
  20. public LittleEndianOutputStream(OutputStream out) {
  21. super(out);
  22. }
  23. @Override
  24. public void writeByte(int v) {
  25. try {
  26. out.write(v);
  27. } catch (IOException e) {
  28. throw new RuntimeException(e);
  29. }
  30. }
  31. @Override
  32. public void writeDouble(double v) {
  33. writeLong(Double.doubleToLongBits(v));
  34. }
  35. @Override
  36. public void writeInt(int v) {
  37. int b3 = (v >>> 24) & 0xFF;
  38. int b2 = (v >>> 16) & 0xFF;
  39. int b1 = (v >>> 8) & 0xFF;
  40. int b0 = (v) & 0xFF;
  41. try {
  42. out.write(b0);
  43. out.write(b1);
  44. out.write(b2);
  45. out.write(b3);
  46. } catch (IOException e) {
  47. throw new RuntimeException(e);
  48. }
  49. }
  50. @Override
  51. public void writeLong(long v) {
  52. writeInt((int)(v/* >> 0*/));
  53. writeInt((int)(v >> 32));
  54. }
  55. @Override
  56. public void writeShort(int v) {
  57. int b1 = (v >>> 8) & 0xFF;
  58. int b0 = (v) & 0xFF;
  59. try {
  60. out.write(b0);
  61. out.write(b1);
  62. } catch (IOException e) {
  63. throw new RuntimeException(e);
  64. }
  65. }
  66. @Override
  67. public void write(byte[] b) {
  68. // suppress IOException for interface method
  69. try {
  70. super.write(b);
  71. } catch (IOException e) {
  72. throw new RuntimeException(e);
  73. }
  74. }
  75. @Override
  76. public void write(byte[] b, int off, int len) {
  77. // suppress IOException for interface method
  78. try {
  79. super.write(b, off, len);
  80. } catch (IOException e) {
  81. throw new RuntimeException(e);
  82. }
  83. }
  84. /**
  85. * Put unsigned int into output stream
  86. *
  87. * @param value
  88. * the int (32-bit) value
  89. */
  90. public void writeUInt( long value ) {
  91. try {
  92. out.write( (byte) ( ( value ) & 0xFF ) );
  93. out.write( (byte) ( ( value >>> 8 ) & 0xFF ) );
  94. out.write( (byte) ( ( value >>> 16 ) & 0xFF ) );
  95. out.write( (byte) ( ( value >>> 24 ) & 0xFF ) );
  96. } catch (IOException e) {
  97. throw new RuntimeException(e);
  98. }
  99. }
  100. /**
  101. * Put unsigned short into output stream
  102. *
  103. * @param value
  104. * the unsigned short (16-bit) value
  105. */
  106. public void putUShort( int value ) {
  107. try {
  108. out.write( (byte) ( ( value ) & 0xFF ) );
  109. out.write( (byte) ( ( value >>> 8 ) & 0xFF ) );
  110. } catch (IOException e) {
  111. throw new RuntimeException(e);
  112. }
  113. }
  114. }