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