1. /*
  2. * @(#)DataOutput.java 1.11 01/11/29
  3. *
  4. * Copyright 2002 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.11, 11/29/01
  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 an <code>long</code> value, which is
  169. * comprised of four 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 >> 48))
  174. * (byte)(0xff & (v >> 40))
  175. * (byte)(0xff & (v >> 32))
  176. * (byte)(0xff & (v >> 24))
  177. * (byte)(0xff & (v >> 16))
  178. * (byte)(0xff & (v >> 8))
  179. * (byte)(0xff & v)
  180. * </code></pre><p>
  181. * The bytes written by this method may be
  182. * read by the <code>readLong</code> method
  183. * of interface <code>DataInput</code> , which
  184. * will then return a <code>long</code> equal
  185. * to <code>v</code>.
  186. *
  187. * @param v the <code>long</code> value to be written.
  188. * @exception IOException if an I/O error occurs.
  189. */
  190. void writeLong(long v) throws IOException;
  191. /**
  192. * Writes a <code>float</code> value,
  193. * which is comprised of four bytes, to the output stream.
  194. * It does this as if it first converts this
  195. * <code>float</code> value to an <code>int</code>
  196. * in exactly the manner of the <code>Float.floatToIntBits</code>
  197. * method and then writes the <code>int</code>
  198. * value in exactly the manner of the <code>writeInt</code>
  199. * method. The bytes written by this method
  200. * may be read by the <code>readFloat</code>
  201. * method of interface <code>DataInput</code>,
  202. * which will then return a <code>float</code>
  203. * equal to <code>v</code>.
  204. *
  205. * @param v the <code>float</code> value to be written.
  206. * @exception IOException if an I/O error occurs.
  207. */
  208. void writeFloat(float v) throws IOException;
  209. /**
  210. * Writes a <code>double</code> value,
  211. * which is comprised of eight bytes, to the output stream.
  212. * It does this as if it first converts this
  213. * <code>double</code> value to a <code>long</code>
  214. * in exactly the manner of the <code>Double.doubleToLongBits</code>
  215. * method and then writes the <code>long</code>
  216. * value in exactly the manner of the <code>writeLong</code>
  217. * method. The bytes written by this method
  218. * may be read by the <code>readDouble</code>
  219. * method of interface <code>DataInput</code>,
  220. * which will then return a <code>double</code>
  221. * equal to <code>v</code>.
  222. *
  223. * @param v the <code>double</code> value to be written.
  224. * @exception IOException if an I/O error occurs.
  225. */
  226. void writeDouble(double v) throws IOException;
  227. /**
  228. * Writes a string to the output stream.
  229. * For every character in the string
  230. * <code>s</code>, taken in order, one byte
  231. * is written to the output stream. If
  232. * <code>s</code> is <code>null</code>, a <code>NullPointerException</code>
  233. * is thrown.<p> If <code>s.length</code>
  234. * is zero, then no bytes are written. Otherwise,
  235. * the character <code>s[0]</code> is written
  236. * first, then <code>s[1]</code>, and so on;
  237. * the last character written is <code>s[s.length-1]</code>.
  238. * For each character, one byte is written,
  239. * the low-order byte, in exactly the manner
  240. * of the <code>writeByte</code> method . The
  241. * high-order eight bits of each character
  242. * in the string are ignored.
  243. *
  244. * @param s the string of bytes to be written.
  245. * @exception IOException if an I/O error occurs.
  246. */
  247. void writeBytes(String s) throws IOException;
  248. /**
  249. * Writes every character in the string <code>s</code>,
  250. * to the output stream, in order,
  251. * two bytes per character. If <code>s</code>
  252. * is <code>null</code>, a <code>NullPointerException</code>
  253. * is thrown. If <code>s.length</code>
  254. * is zero, then no characters are written.
  255. * Otherwise, the character <code>s[0]</code>
  256. * is written first, then <code>s[1]</code>,
  257. * and so on; the last character written is
  258. * <code>s[s.length-1]</code>. For each character,
  259. * two bytes are actually written, high-order
  260. * byte first, in exactly the manner of the
  261. * <code>writeChar</code> method.
  262. *
  263. * @param s the string value to be written.
  264. * @exception IOException if an I/O error occurs.
  265. */
  266. void writeChars(String s) throws IOException;
  267. /**
  268. * Writes two bytes of length information
  269. * to the output stream, followed
  270. * by the Java modified UTF representation
  271. * of every character in the string <code>s</code>.
  272. * If <code>s</code> is <code>null</code>,
  273. * a <code>NullPointerException</code> is thrown.
  274. * Each character in the string <code>s</code>
  275. * is converted to a group of one, two, or
  276. * three bytes, depending on the value of the
  277. * character.<p>
  278. * If a character <code>c</code>
  279. * is in the range <code>\u0001</code> through
  280. * <code>\u007f</code>, it is represented
  281. * by one byte:<p>
  282. * <pre>(byte)c </pre> <p>
  283. * If a character <code>c</code> is <code>\u0000</code>
  284. * or is in the range <code>\u0080</code>
  285. * through <code>\u07ff</code>, then it is
  286. * represented by two bytes, to be written
  287. * in the order shown:<p> <pre><code>
  288. * (byte)(0xc0 | (0x1f & (c >> 6)))
  289. * (byte)(0x80 | (0x3f & c))
  290. * </code></pre> <p> If a character
  291. * <code>c</code> is in the range <code>\u0800</code>
  292. * through <code>uffff</code>, then it is
  293. * represented by three bytes, to be written
  294. * in the order shown:<p> <pre><code>
  295. * (byte)(0xc0 | (0x0f & (c >> 12)))
  296. * (byte)(0x80 | (0x3f & (c >> 6)))
  297. * (byte)(0x80 | (0x3f & c))
  298. * </code></pre> <p> First,
  299. * the total number of bytes needed to represent
  300. * all the characters of <code>s</code> is
  301. * calculated. If this number is larger than
  302. * <code>65535</code>, then a <code>UTFDataFormatError</code>
  303. * is thrown. Otherwise, this length is written
  304. * to the output stream in exactly the manner
  305. * of the <code>writeShort</code> method;
  306. * after this, the one-, two-, or three-byte
  307. * representation of each character in the
  308. * string <code>s</code> is written.<p> The
  309. * bytes written by this method may be read
  310. * by the <code>readUTF</code> method of interface
  311. * <code>DataInput</code> , which will then
  312. * return a <code>String</code> equal to <code>s</code>.
  313. *
  314. * @param str the string value to be written.
  315. * @exception IOException if an I/O error occurs.
  316. */
  317. void writeUTF(String str) throws IOException;
  318. }