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

TypeWriter.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.hpsf;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import org.apache.poi.util.LittleEndian;
  19. /**
  20. * <p>Class for writing little-endian data and more.</p>
  21. *
  22. * @author Rainer Klute <a
  23. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  24. */
  25. public class TypeWriter
  26. {
  27. /**
  28. * <p>Writes a two-byte value (short) to an output stream.</p>
  29. *
  30. * @param out The stream to write to.
  31. * @param n The value to write.
  32. * @return The number of bytes that have been written.
  33. * @exception IOException if an I/O error occurs
  34. */
  35. public static int writeToStream(final OutputStream out, final short n)
  36. throws IOException
  37. {
  38. final int length = LittleEndian.SHORT_SIZE;
  39. byte[] buffer = new byte[length];
  40. LittleEndian.putShort(buffer, 0, n); // FIXME: unsigned
  41. out.write(buffer, 0, length);
  42. return length;
  43. }
  44. /**
  45. * <p>Writes a four-byte value to an output stream.</p>
  46. *
  47. * @param out The stream to write to.
  48. * @param n The value to write.
  49. * @exception IOException if an I/O error occurs
  50. * @return The number of bytes written to the output stream.
  51. */
  52. public static int writeToStream(final OutputStream out, final int n)
  53. throws IOException
  54. {
  55. final int l = LittleEndian.INT_SIZE;
  56. final byte[] buffer = new byte[l];
  57. LittleEndian.putInt(buffer, 0, n);
  58. out.write(buffer, 0, l);
  59. return l;
  60. }
  61. /**
  62. * <p>Writes a eight-byte value to an output stream.</p>
  63. *
  64. * @param out The stream to write to.
  65. * @param n The value to write.
  66. * @exception IOException if an I/O error occurs
  67. * @return The number of bytes written to the output stream.
  68. */
  69. public static int writeToStream(final OutputStream out, final long n)
  70. throws IOException
  71. {
  72. final int l = LittleEndian.LONG_SIZE;
  73. final byte[] buffer = new byte[l];
  74. LittleEndian.putLong(buffer, 0, n);
  75. out.write(buffer, 0, l);
  76. return l;
  77. }
  78. /**
  79. * <p>Writes an unsigned two-byte value to an output stream.</p>
  80. *
  81. * @param out The stream to write to
  82. * @param n The value to write
  83. * @exception IOException if an I/O error occurs
  84. */
  85. public static void writeUShortToStream(final OutputStream out, final int n)
  86. throws IOException
  87. {
  88. int high = n & 0xFFFF0000;
  89. if (high != 0)
  90. throw new IllegalPropertySetDataException
  91. ("Value " + n + " cannot be represented by 2 bytes.");
  92. writeToStream(out, (short) n);
  93. }
  94. /**
  95. * <p>Writes an unsigned four-byte value to an output stream.</p>
  96. *
  97. * @param out The stream to write to.
  98. * @param n The value to write.
  99. * @return The number of bytes that have been written to the output stream.
  100. * @exception IOException if an I/O error occurs
  101. */
  102. public static int writeUIntToStream(final OutputStream out, final long n)
  103. throws IOException
  104. {
  105. long high = n & 0xFFFFFFFF00000000L;
  106. if (high != 0 && high != 0xFFFFFFFF00000000L)
  107. throw new IllegalPropertySetDataException
  108. ("Value " + n + " cannot be represented by 4 bytes.");
  109. return writeToStream(out, (int) n);
  110. }
  111. /**
  112. * <p>Writes a 16-byte {@link ClassID} to an output stream.</p>
  113. *
  114. * @param out The stream to write to
  115. * @param n The value to write
  116. * @return The number of bytes written
  117. * @exception IOException if an I/O error occurs
  118. */
  119. public static int writeToStream(final OutputStream out, final ClassID n)
  120. throws IOException
  121. {
  122. byte[] b = new byte[16];
  123. n.write(b, 0);
  124. out.write(b, 0, b.length);
  125. return b.length;
  126. }
  127. /**
  128. * <p>Writes an array of {@link Property} instances to an output stream
  129. * according to the Horrible Property Stream Format.</p>
  130. *
  131. * @param out The stream to write to
  132. * @param properties The array to write to the stream
  133. * @param codepage The codepage number to use for writing strings
  134. * @exception IOException if an I/O error occurs
  135. * @throws UnsupportedVariantTypeException if HPSF does not support some
  136. * variant type.
  137. */
  138. public static void writeToStream(final OutputStream out,
  139. final Property[] properties,
  140. final int codepage)
  141. throws IOException, UnsupportedVariantTypeException
  142. {
  143. /* If there are no properties don't write anything. */
  144. if (properties == null)
  145. return;
  146. /* Write the property list. This is a list containing pairs of property
  147. * ID and offset into the stream. */
  148. for (int i = 0; i < properties.length; i++)
  149. {
  150. final Property p = properties[i];
  151. writeUIntToStream(out, p.getID());
  152. writeUIntToStream(out, p.getSize());
  153. }
  154. /* Write the properties themselves. */
  155. for (int i = 0; i < properties.length; i++)
  156. {
  157. final Property p = properties[i];
  158. long type = p.getType();
  159. writeUIntToStream(out, type);
  160. VariantSupport.write(out, (int) type, p.getValue(), codepage);
  161. }
  162. }
  163. /**
  164. * <p>Writes a double value value to an output stream.</p>
  165. *
  166. * @param out The stream to write to.
  167. * @param n The value to write.
  168. * @exception IOException if an I/O error occurs
  169. * @return The number of bytes written to the output stream.
  170. */
  171. public static int writeToStream(final OutputStream out, final double n)
  172. throws IOException
  173. {
  174. final int l = LittleEndian.DOUBLE_SIZE;
  175. final byte[] buffer = new byte[l];
  176. LittleEndian.putDouble(buffer, 0, n);
  177. out.write(buffer, 0, l);
  178. return l;
  179. }
  180. }