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