PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hpsf/TypeWriter.java

http://github.com/openmicroscopy/bioformats
Java | 238 lines | 90 code | 32 blank | 116 comment | 10 complexity | 638f11ecafb414834c45ab757ac19a8d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hpsf;
  38. import java.io.IOException;
  39. import java.io.OutputStream;
  40. import loci.poi.util.LittleEndian;
  41. /**
  42. * <p>Class for writing little-endian data and more.</p>
  43. *
  44. * @author Rainer Klute <a
  45. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  46. * @version $Id: TypeWriter.java 489730 2006-12-22 19:18:16Z bayard $
  47. * @since 2003-02-20
  48. */
  49. public class TypeWriter
  50. {
  51. /**
  52. * <p>Writes a two-byte value (short) to an output stream.</p>
  53. *
  54. * @param out The stream to write to.
  55. * @param n The value to write.
  56. * @return The number of bytes that have been written.
  57. * @exception IOException if an I/O error occurs
  58. */
  59. public static int writeToStream(final OutputStream out, final short n)
  60. throws IOException
  61. {
  62. final int length = LittleEndian.SHORT_SIZE;
  63. byte[] buffer = new byte[length];
  64. LittleEndian.putShort(buffer, 0, n); // FIXME: unsigned
  65. out.write(buffer, 0, length);
  66. return length;
  67. }
  68. /**
  69. * <p>Writes a four-byte value to an output stream.</p>
  70. *
  71. * @param out The stream to write to.
  72. * @param n The value to write.
  73. * @exception IOException if an I/O error occurs
  74. * @return The number of bytes written to the output stream.
  75. */
  76. public static int writeToStream(final OutputStream out, final int n)
  77. throws IOException
  78. {
  79. final int l = LittleEndian.INT_SIZE;
  80. final byte[] buffer = new byte[l];
  81. LittleEndian.putInt(buffer, 0, n);
  82. out.write(buffer, 0, l);
  83. return l;
  84. }
  85. /**
  86. * <p>Writes a eight-byte value to an output stream.</p>
  87. *
  88. * @param out The stream to write to.
  89. * @param n The value to write.
  90. * @exception IOException if an I/O error occurs
  91. * @return The number of bytes written to the output stream.
  92. */
  93. public static int writeToStream(final OutputStream out, final long n)
  94. throws IOException
  95. {
  96. final int l = LittleEndian.LONG_SIZE;
  97. final byte[] buffer = new byte[l];
  98. LittleEndian.putLong(buffer, 0, n);
  99. out.write(buffer, 0, l);
  100. return l;
  101. }
  102. /**
  103. * <p>Writes an unsigned two-byte value to an output stream.</p>
  104. *
  105. * @param out The stream to write to
  106. * @param n The value to write
  107. * @exception IOException if an I/O error occurs
  108. */
  109. public static void writeUShortToStream(final OutputStream out, final int n)
  110. throws IOException
  111. {
  112. int high = n & 0xFFFF0000;
  113. if (high != 0)
  114. throw new IllegalPropertySetDataException
  115. ("Value " + n + " cannot be represented by 2 bytes.");
  116. writeToStream(out, (short) n);
  117. }
  118. /**
  119. * <p>Writes an unsigned four-byte value to an output stream.</p>
  120. *
  121. * @param out The stream to write to.
  122. * @param n The value to write.
  123. * @return The number of bytes that have been written to the output stream.
  124. * @exception IOException if an I/O error occurs
  125. */
  126. public static int writeUIntToStream(final OutputStream out, final long n)
  127. throws IOException
  128. {
  129. long high = n & 0xFFFFFFFF00000000L;
  130. if (high != 0 && high != 0xFFFFFFFF00000000L)
  131. throw new IllegalPropertySetDataException
  132. ("Value " + n + " cannot be represented by 4 bytes.");
  133. return writeToStream(out, (int) n);
  134. }
  135. /**
  136. * <p>Writes a 16-byte {@link ClassID} to an output stream.</p>
  137. *
  138. * @param out The stream to write to
  139. * @param n The value to write
  140. * @return The number of bytes written
  141. * @exception IOException if an I/O error occurs
  142. */
  143. public static int writeToStream(final OutputStream out, final ClassID n)
  144. throws IOException
  145. {
  146. byte[] b = new byte[16];
  147. n.write(b, 0);
  148. out.write(b, 0, b.length);
  149. return b.length;
  150. }
  151. /**
  152. * <p>Writes an array of {@link Property} instances to an output stream
  153. * according to the Horrible Property Stream Format.</p>
  154. *
  155. * @param out The stream to write to
  156. * @param properties The array to write to the stream
  157. * @param codepage The codepage number to use for writing strings
  158. * @exception IOException if an I/O error occurs
  159. * @throws UnsupportedVariantTypeException if HPSF does not support some
  160. * variant type.
  161. */
  162. public static void writeToStream(final OutputStream out,
  163. final Property[] properties,
  164. final int codepage)
  165. throws IOException, UnsupportedVariantTypeException
  166. {
  167. /* If there are no properties don't write anything. */
  168. if (properties == null)
  169. return;
  170. /* Write the property list. This is a list containing pairs of property
  171. * ID and offset into the stream. */
  172. for (int i = 0; i < properties.length; i++)
  173. {
  174. final Property p = properties[i];
  175. writeUIntToStream(out, p.getID());
  176. writeUIntToStream(out, p.getSize());
  177. }
  178. /* Write the properties themselves. */
  179. for (int i = 0; i < properties.length; i++)
  180. {
  181. final Property p = properties[i];
  182. long type = p.getType();
  183. writeUIntToStream(out, type);
  184. VariantSupport.write(out, (int) type, p.getValue(), codepage);
  185. }
  186. }
  187. /**
  188. * <p>Writes a double value value to an output stream.</p>
  189. *
  190. * @param out The stream to write to.
  191. * @param n The value to write.
  192. * @exception IOException if an I/O error occurs
  193. * @return The number of bytes written to the output stream.
  194. */
  195. public static int writeToStream(final OutputStream out, final double n)
  196. throws IOException
  197. {
  198. final int l = LittleEndian.DOUBLE_SIZE;
  199. final byte[] buffer = new byte[l];
  200. LittleEndian.putDouble(buffer, 0, n);
  201. out.write(buffer, 0, l);
  202. return l;
  203. }
  204. }