1. /*
  2. * @(#)DataOutput.java 1.18 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * The <code>DataOutput</code> interface provides
  10. * for converting data from any of the Java
  11. * primitive types to a series of bytes and
  12. * writing these bytes to a binary stream.
  13. * There is also a facility for converting
  14. * a <code>String</code> into Java modified
  15. * UTF-8 format and writing the resulting series
  16. * of bytes.
  17. * <p>
  18. * For all the methods in this interface that
  19. * write bytes, it is generally true that if
  20. * a byte cannot be written for any reason,
  21. * an <code>IOException</code> is thrown.
  22. *
  23. * @author Frank Yellin
  24. * @version 1.18, 01/23/03
  25. * @see java.io.DataInput
  26. * @see java.io.DataOutputStream
  27. * @since JDK1.0
  28. */
  29. public
  30. interface DataOutput {
  31. /**
  32. * Writes to the output stream the eight
  33. * low-order bits of the argument <code>b</code>.
  34. * The 24 high-order bits of <code>b</code>
  35. * are ignored.
  36. *
  37. * @param b the byte to be written.
  38. * @exception IOException if an I/O error occurs.
  39. */
  40. void write(int b) throws IOException;
  41. /**
  42. * Writes to the output stream all the bytes in array <code>b</code>.
  43. * If <code>b</code> is <code>null</code>,
  44. * a <code>NullPointerException</code> is thrown.
  45. * If <code>b.length</code> is zero, then
  46. * no bytes are written. Otherwise, the byte
  47. * <code>b[0]</code> is written first, then
  48. * <code>b[1]</code>, and so on; the last byte
  49. * written is <code>b[b.length-1]</code>.
  50. *
  51. * @param b the data.
  52. * @exception IOException if an I/O error occurs.
  53. */
  54. void write(byte b[]) throws IOException;
  55. /**
  56. * Writes <code>len</code> bytes from array
  57. * <code>b</code>, in order, to
  58. * the output stream. If <code>b</code>
  59. * is <code>null</code>, a <code>NullPointerException</code>
  60. * is thrown. If <code>off</code> is negative,
  61. * or <code>len</code> is negative, or <code>off+len</code>
  62. * is greater than the length of the array
  63. * <code>b</code>, then an <code>IndexOutOfBoundsException</code>
  64. * is thrown. If <code>len</code> is zero,
  65. * then no bytes are written. Otherwise, the
  66. * byte <code>b[off]</code> is written first,
  67. * then <code>b[off+1]</code>, and so on; the
  68. * last byte written is <code>b[off+len-1]</code>.
  69. *
  70. * @param b the data.
  71. * @param off the start offset in the data.
  72. * @param len the number of bytes to write.
  73. * @exception IOException if an I/O error occurs.
  74. */
  75. void write(byte b[], int off, int len) throws IOException;
  76. /**
  77. * Writes a <code>boolean</code> value to this output stream.
  78. * If the argument <code>v</code>
  79. * is <code>true</code>, the value <code>(byte)1</code>
  80. * is written; if <code>v</code> is <code>false</code>,
  81. * the value <code>(byte)0</code> is written.
  82. * The byte written by this method may
  83. * be read by the <code>readBoolean</code>
  84. * method of interface <code>DataInput</code>,
  85. * which will then return a <code>boolean</code>
  86. * equal to <code>v</code>.
  87. *
  88. * @param v the boolean to be written.
  89. * @exception IOException if an I/O error occurs.
  90. */
  91. void writeBoolean(boolean v) throws IOException;
  92. /**
  93. * Writes to the output stream the eight low-
  94. * order bits of the argument <code>v</code>.
  95. * The 24 high-order bits of <code>v</code>
  96. * are ignored. (This means that <code>writeByte</code>
  97. * does exactly the same thing as <code>write</code>
  98. * for an integer argument.) The byte written
  99. * by this method may be read by the <code>readByte</code>
  100. * method of interface <code>DataInput</code>,
  101. * which will then return a <code>byte</code>
  102. * equal to <code>(byte)v</code>.
  103. *
  104. * @param v the byte value to be written.
  105. * @exception IOException if an I/O error occurs.
  106. */
  107. void writeByte(int v) throws IOException;
  108. /**
  109. * Writes two bytes to the output
  110. * stream to represent the value of the argument.
  111. * The byte values to be written, in the order
  112. * shown, are: <p>
  113. * <pre><code>
  114. * (byte)(0xff & (v >> 8))
  115. * (byte)(0xff & v)
  116. * </code> </pre> <p>
  117. * The bytes written by this method may be
  118. * read by the <code>readShort</code> method
  119. * of interface <code>DataInput</code> , which
  120. * will then return a <code>short</code> equal
  121. * to <code>(short)v</code>.
  122. *
  123. * @param v the <code>short</code> value to be written.
  124. * @exception IOException if an I/O error occurs.
  125. */
  126. void writeShort(int v) throws IOException;
  127. /**
  128. * Writes a <code>char</code> value, wich
  129. * is comprised of two bytes, to the
  130. * output stream.
  131. * The byte values to be written, in the order
  132. * shown, are:
  133. * <p><pre><code>
  134. * (byte)(0xff & (v >> 8))
  135. * (byte)(0xff & v)
  136. * </code></pre><p>
  137. * The bytes written by this method may be
  138. * read by the <code>readChar</code> method
  139. * of interface <code>DataInput</code> , which
  140. * will then return a <code>char</code> equal
  141. * to <code>(char)v</code>.
  142. *
  143. * @param v the <code>char</code> value to be written.
  144. * @exception IOException if an I/O error occurs.
  145. */
  146. void writeChar(int v) throws IOException;
  147. /**
  148. * Writes an <code>int</code> value, which is
  149. * comprised of four bytes, to the output stream.
  150. * The byte values to be written, in the order
  151. * shown, are:
  152. * <p><pre><code>
  153. * (byte)(0xff & (v >> 24))
  154. * (byte)(0xff & (v >> 16))
  155. * (byte)(0xff & (v >> 8))
  156. * (byte)(0xff & v)
  157. * </code></pre><p>
  158. * The bytes written by this method may be read
  159. * by the <code>readInt</code> method of interface
  160. * <code>DataInput</code> , which will then
  161. * return an <code>int</code> equal to <code>v</code>.
  162. *
  163. * @param v the <code>int</code> value to be written.
  164. * @exception IOException if an I/O error occurs.
  165. */
  166. void writeInt(int v) throws IOException;
  167. /**
  168. * Writes a <code>long</code> value, which is
  169. * comprised of eight bytes, to the output stream.
  170. * The byte values to be written, in the order
  171. * shown, are:
  172. * <p><pre><code>
  173. * (byte)(0xff & (v >> 56))
  174. * (byte)(0xff & (v >> 48))
  175. * (byte)(0xff & (v >> 40))
  176. * (byte)(0xff & (v >> 32))
  177. * (byte)(0xff & (v >> 24))
  178. * (byte)(0xff & (v >> 16))
  179. * (byte)(0xff & (v >> 8))
  180. * (byte)(0xff & v)
  181. * </code></pre><p>
  182. * The bytes written by this method may be
  183. * read by the <code>readLong</code> method
  184. * of interface <code>DataInput</code> , which
  185. * will then return a <code>long</code> equal
  186. * to <code>v</code>.
  187. *
  188. * @param v the <code>long</code> value to be written.
  189. * @exception IOException if an I/O error occurs.
  190. */
  191. void writeLong(long v) throws IOException;
  192. /**
  193. * Writes a <code>float</code> value,
  194. * which is comprised of four bytes, to the output stream.
  195. * It does this as if it first converts this
  196. * <code>float</code> value to an <code>int</code>
  197. * in exactly the manner of the <code>Float.floatToIntBits</code>
  198. * method and then writes the <code>int</code>
  199. * value in exactly the manner of the <code>writeInt</code>
  200. * method. The bytes written by this method
  201. * may be read by the <code>readFloat</code>
  202. * method of interface <code>DataInput</code>,
  203. * which will then return a <code>float</code>
  204. * equal to <code>v</code>.
  205. *
  206. * @param v the <code>float</code> value to be written.
  207. * @exception IOException if an I/O error occurs.
  208. */
  209. void writeFloat(float v) throws IOException;
  210. /**
  211. * Writes a <code>double</code> value,
  212. * which is comprised of eight bytes, to the output stream.
  213. * It does this as if it first converts this
  214. * <code>double</code> value to a <code>long</code>
  215. * in exactly the manner of the <code>Double.doubleToLongBits</code>
  216. * method and then writes the <code>long</code>
  217. * value in exactly the manner of the <code>writeLong</code>
  218. * method. The bytes written by this method
  219. * may be read by the <code>readDouble</code>
  220. * method of interface <code>DataInput</code>,
  221. * which will then return a <code>double</code>
  222. * equal to <code>v</code>.
  223. *
  224. * @param v the <code>double</code> value to be written.
  225. * @exception IOException if an I/O error occurs.
  226. */
  227. void writeDouble(double v) throws IOException;
  228. /**
  229. * Writes a string to the output stream.
  230. * For every character in the string
  231. * <code>s</code>, taken in order, one byte
  232. * is written to the output stream. If
  233. * <code>s</code> is <code>null</code>, a <code>NullPointerException</code>
  234. * is thrown.<p> If <code>s.length</code>
  235. * is zero, then no bytes are written. Otherwise,
  236. * the character <code>s[0]</code> is written
  237. * first, then <code>s[1]</code>, and so on;
  238. * the last character written is <code>s[s.length-1]</code>.
  239. * For each character, one byte is written,
  240. * the low-order byte, in exactly the manner
  241. * of the <code>writeByte</code> method . The
  242. * high-order eight bits of each character
  243. * in the string are ignored.
  244. *
  245. * @param s the string of bytes to be written.
  246. * @exception IOException if an I/O error occurs.
  247. */
  248. void writeBytes(String s) throws IOException;
  249. /**
  250. * Writes every character in the string <code>s</code>,
  251. * to the output stream, in order,
  252. * two bytes per character. If <code>s</code>
  253. * is <code>null</code>, a <code>NullPointerException</code>
  254. * is thrown. If <code>s.length</code>
  255. * is zero, then no characters are written.
  256. * Otherwise, the character <code>s[0]</code>
  257. * is written first, then <code>s[1]</code>,
  258. * and so on; the last character written is
  259. * <code>s[s.length-1]</code>. For each character,
  260. * two bytes are actually written, high-order
  261. * byte first, in exactly the manner of the
  262. * <code>writeChar</code> method.
  263. *
  264. * @param s the string value to be written.
  265. * @exception IOException if an I/O error occurs.
  266. */
  267. void writeChars(String s) throws IOException;
  268. /**
  269. * Writes two bytes of length information
  270. * to the output stream, followed
  271. * by the Java modified UTF representation
  272. * of every character in the string <code>s</code>.
  273. * If <code>s</code> is <code>null</code>,
  274. * a <code>NullPointerException</code> is thrown.
  275. * Each character in the string <code>s</code>
  276. * is converted to a group of one, two, or
  277. * three bytes, depending on the value of the
  278. * character.<p>
  279. * If a character <code>c</code>
  280. * is in the range <code>\u0001</code> through
  281. * <code>\u007f</code>, it is represented
  282. * by one byte:<p>
  283. * <pre>(byte)c </pre> <p>
  284. * If a character <code>c</code> is <code>\u0000</code>
  285. * or is in the range <code>\u0080</code>
  286. * through <code>\u07ff</code>, then it is
  287. * represented by two bytes, to be written
  288. * in the order shown:<p> <pre><code>
  289. * (byte)(0xc0 | (0x1f & (c >> 6)))
  290. * (byte)(0x80 | (0x3f & c))
  291. * </code></pre> <p> If a character
  292. * <code>c</code> is in the range <code>\u0800</code>
  293. * through <code>uffff</code>, then it is
  294. * represented by three bytes, to be written
  295. * in the order shown:<p> <pre><code>
  296. * (byte)(0xe0 | (0x0f & (c >> 12)))
  297. * (byte)(0x80 | (0x3f & (c >> 6)))
  298. * (byte)(0x80 | (0x3f & c))
  299. * </code></pre> <p> First,
  300. * the total number of bytes needed to represent
  301. * all the characters of <code>s</code> is
  302. * calculated. If this number is larger than
  303. * <code>65535</code>, then a <code>UTFDataFormatException</code>
  304. * is thrown. Otherwise, this length is written
  305. * to the output stream in exactly the manner
  306. * of the <code>writeShort</code> method;
  307. * after this, the one-, two-, or three-byte
  308. * representation of each character in the
  309. * string <code>s</code> is written.<p> The
  310. * bytes written by this method may be read
  311. * by the <code>readUTF</code> method of interface
  312. * <code>DataInput</code> , which will then
  313. * return a <code>String</code> equal to <code>s</code>.
  314. *
  315. * @param str the string value to be written.
  316. * @exception IOException if an I/O error occurs.
  317. */
  318. void writeUTF(String str) throws IOException;
  319. }