DataOutput.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* DataOutput.java -- Interface for writing data from a stream
  2. Copyright (C) 1998, 1999, 2001, 2003, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  33. * "The Java Language Specification", ISBN 0-201-63451-1
  34. * Status: Complete to version 1.1.
  35. */
  36. /**
  37. * This interface is implemented by classes that can wrte data to streams
  38. * from Java primitive types. This data can subsequently be read back
  39. * by classes implementing the <code>DataInput</code> interface.
  40. *
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. * @author Tom Tromey (tromey@cygnus.com)
  43. *
  44. * @see DataInput
  45. */
  46. public interface DataOutput
  47. {
  48. /**
  49. * This method writes a Java boolean value to an output stream. If
  50. * <code>value</code> is <code>true</code>, a byte with the value of
  51. * 1 will be written, otherwise a byte with the value of 0 will be
  52. * written.
  53. *
  54. * The value written can be read using the <code>readBoolean</code>
  55. * method in <code>DataInput</code>.
  56. *
  57. * @param value The boolean value to write
  58. *
  59. * @exception IOException If an error occurs
  60. *
  61. * @see DataInput#readBoolean
  62. */
  63. void writeBoolean(boolean value) throws IOException;
  64. /**
  65. * This method writes a Java byte value to an output stream. The
  66. * byte to be written will be in the lowest 8 bits of the
  67. * <code>int</code> value passed.
  68. *
  69. * The value written can be read using the <code>readByte</code> or
  70. * <code>readUnsignedByte</code> methods in <code>DataInput</code>.
  71. *
  72. * @param value The int value to write
  73. *
  74. * @exception IOException If an error occurs
  75. *
  76. * @see DataInput#readByte
  77. * @see DataInput#readUnsignedByte
  78. */
  79. void writeByte(int value) throws IOException;
  80. /**
  81. * This method writes a Java char value to an output stream. The
  82. * char to be written will be in the lowest 16 bits of the <code>int</code>
  83. * value passed. These bytes will be written "big endian". That is,
  84. * with the high byte written first in the following manner:
  85. * <p>
  86. * <code>byte0 = (byte)((value & 0xFF00) >> 8);<br>
  87. * byte1 = (byte)(value & 0x00FF);</code>
  88. * <p>
  89. *
  90. * The value written can be read using the <code>readChar</code>
  91. * method in <code>DataInput</code>.
  92. *
  93. * @param value The char value to write
  94. *
  95. * @exception IOException If an error occurs
  96. *
  97. * @see DataInput#readChar
  98. */
  99. void writeChar(int value) throws IOException;
  100. /**
  101. * This method writes a Java short value to an output stream. The
  102. * char to be written will be in the lowest 16 bits of the <code>int</code>
  103. * value passed. These bytes will be written "big endian". That is,
  104. * with the high byte written first in the following manner:
  105. * <p>
  106. * <code>byte0 = (byte)((value & 0xFF00) >> 8);<br>
  107. * byte1 = (byte)(value & 0x00FF);</code>
  108. * <p>
  109. *
  110. * The value written can be read using the <code>readShort</code> and
  111. * <code>readUnsignedShort</code> methods in <code>DataInput</code>.
  112. *
  113. * @param value The int value to write as a 16-bit value
  114. *
  115. * @exception IOException If an error occurs
  116. *
  117. * @see DataInput#readShort
  118. * @see DataInput#readUnsignedShort
  119. */
  120. void writeShort(int value) throws IOException;
  121. /**
  122. * This method writes a Java int value to an output stream. The 4 bytes
  123. * of the passed value will be written "big endian". That is, with
  124. * the high byte written first in the following manner:
  125. * <p>
  126. * <code>byte0 = (byte)((value & 0xFF000000) >> 24);<br>
  127. * byte1 = (byte)((value & 0x00FF0000) >> 16);<br>
  128. * byte2 = (byte)((value & 0x0000FF00) >> 8);<br>
  129. * byte3 = (byte)(value & 0x000000FF);</code>
  130. * <p>
  131. *
  132. * The value written can be read using the <code>readInt</code>
  133. * method in <code>DataInput</code>.
  134. *
  135. * @param value The int value to write
  136. *
  137. * @exception IOException If an error occurs
  138. *
  139. * @see DataInput#readInt
  140. */
  141. void writeInt(int value) throws IOException;
  142. /**
  143. * This method writes a Java long value to an output stream. The 8 bytes
  144. * of the passed value will be written "big endian". That is, with
  145. * the high byte written first in the following manner:
  146. * <p>
  147. * <code>byte0 = (byte)((value & 0xFF00000000000000L) >> 56);<br>
  148. * byte1 = (byte)((value & 0x00FF000000000000L) >> 48);<br>
  149. * byte2 = (byte)((value & 0x0000FF0000000000L) >> 40);<br>
  150. * byte3 = (byte)((value & 0x000000FF00000000L) >> 32);<br>
  151. * byte4 = (byte)((value & 0x00000000FF000000L) >> 24);<br>
  152. * byte5 = (byte)((value & 0x0000000000FF0000L) >> 16);<br>
  153. * byte6 = (byte)((value & 0x000000000000FF00L) >> 8);<br>
  154. * byte7 = (byte)(value & 0x00000000000000FFL);</code>
  155. * <p>
  156. *
  157. * The value written can be read using the <code>readLong</code>
  158. * method in <code>DataInput</code>.
  159. *
  160. * @param value The long value to write
  161. *
  162. * @exception IOException If an error occurs
  163. *
  164. * @see DataInput#readLong
  165. */
  166. void writeLong(long value) throws IOException;
  167. /**
  168. * This method writes a Java <code>float</code> value to the stream. This
  169. * value is written by first calling the method
  170. * <code>Float.floatToIntBits</code>
  171. * to retrieve an <code>int</code> representing the floating point number,
  172. * then writing this <code>int</code> value to the stream exactly the same
  173. * as the <code>writeInt()</code> method does.
  174. *
  175. * The value written can be read using the <code>readFloat</code>
  176. * method in <code>DataInput</code>.
  177. *
  178. * @param value The float value to write
  179. *
  180. * @exception IOException If an error occurs
  181. *
  182. * @see #writeInt
  183. * @see DataInput#readFloat
  184. * @see Float#floatToIntBits
  185. */
  186. void writeFloat(float value) throws IOException;
  187. /**
  188. * This method writes a Java <code>double</code> value to the stream. This
  189. * value is written by first calling the method
  190. * <code>Double.doubleToLongBits</code>
  191. * to retrieve an <code>long</code> representing the floating point number,
  192. * then writing this <code>long</code> value to the stream exactly the same
  193. * as the <code>writeLong()</code> method does.
  194. *
  195. * The value written can be read using the <code>readDouble</code>
  196. * method in <code>DataInput</code>.
  197. *
  198. * @param value The double value to write
  199. *
  200. * @exception IOException If any other error occurs
  201. *
  202. * @see #writeLong
  203. * @see DataInput#readDouble
  204. * @see Double#doubleToLongBits
  205. */
  206. void writeDouble(double value) throws IOException;
  207. /**
  208. * This method writes all the bytes in a <code>String</code> out to the
  209. * stream. One byte is written for each character in the
  210. * <code>String</code>.
  211. * The high eight bits of each character are discarded, thus this
  212. * method is inappropriate for completely representing Unicode characters.
  213. *
  214. * @param value The <code>String</code> to write
  215. *
  216. * @exception IOException If an error occurs
  217. */
  218. void writeBytes(String value) throws IOException;
  219. /**
  220. * This method writes all the characters of a <code>String</code> to an
  221. * output stream as an array of <code>char</code>'s. Each character
  222. * is written using the method specified in the <code>writeChar</code>
  223. * method.
  224. *
  225. * @param value The String to write
  226. *
  227. * @exception IOException If an error occurs
  228. *
  229. * @see #writeChar(int)
  230. */
  231. void writeChars(String value) throws IOException;
  232. /**
  233. * This method writes a Java <code>String</code> to the stream in a modified
  234. * UTF-8 format. First, two bytes are written to the stream indicating the
  235. * number of bytes to follow. This is written in the form of a Java
  236. * <code>short</code> value in the same manner used by the
  237. * <code>writeShort</code> method. Note that this is the number of
  238. * bytes in the
  239. * encoded <code>String</code> not the <code>String</code> length. Next
  240. * come the encoded characters. Each character in the <code>String</code>
  241. * is encoded as either one, two or three bytes. For characters in the
  242. * range of <code>\u0001</code> to <code>\u007F</code>, one byte is used.
  243. * The character
  244. * value goes into bits 0-7 and bit eight is 0. For characters in the range
  245. * of <code>\u0080</code> to <code>\u007FF</code>, two bytes are used. Bits
  246. * 6-10 of the character value are encoded bits 0-4 of the first byte, with
  247. * the high bytes having a value of "110". Bits 0-5 of the character value
  248. * are stored in bits 0-5 of the second byte, with the high bits set to
  249. * "10". This type of encoding is also done for the null character
  250. * <code>\u0000</code>. This eliminates any C style NUL character values
  251. * in the output. All remaining characters are stored as three bytes.
  252. * Bits 12-15 of the character value are stored in bits 0-3 of the first
  253. * byte. The high bits of the first bytes are set to "1110". Bits 6-11
  254. * of the character value are stored in bits 0-5 of the second byte. The
  255. * high bits of the second byte are set to "10". And bits 0-5 of the
  256. * character value are stored in bits 0-5 of byte three, with the high bits
  257. * of that byte set to "10".
  258. *
  259. * The value written can be read using the <code>readUTF</code>
  260. * method in <code>DataInput</code>.
  261. *
  262. * @param value The <code>String</code> to write
  263. *
  264. * @exception IOException If an error occurs
  265. *
  266. * @see DataInput#readUTF
  267. */
  268. void writeUTF(String value) throws IOException;
  269. /**
  270. * This method writes an 8-bit value (passed into the method as a Java
  271. * <code>int</code>) to an output stream. The low 8 bits of the
  272. * passed value are written.
  273. *
  274. * @param value The <code>byte</code> to write to the output stream
  275. *
  276. * @exception IOException If an error occurs
  277. */
  278. void write(int value) throws IOException;
  279. /**
  280. * This method writes the raw byte array passed in to the output stream.
  281. *
  282. * @param buf The byte array to write
  283. *
  284. * @exception IOException If an error occurs
  285. */
  286. void write(byte[] buf) throws IOException;
  287. /**
  288. * This method writes raw bytes from the passed array <code>buf</code>
  289. * starting
  290. * <code>offset</code> bytes into the buffer. The number of bytes
  291. * written will be exactly <code>len</code>.
  292. *
  293. * @param buf The buffer from which to write the data
  294. * @param offset The offset into the buffer to start writing data from
  295. * @param len The number of bytes to write from the buffer to the output
  296. * stream
  297. *
  298. * @exception IOException If any other error occurs
  299. */
  300. void write(byte[] buf, int offset, int len) throws IOException;
  301. } // interface DataOutput