Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/DataOutput.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* The <code>DataOutput</code> interface provides29* for converting data from any of the Java30* primitive types to a series of bytes and31* writing these bytes to a binary stream.32* There is also a facility for converting33* a <code>String</code> into34* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>35* format and writing the resulting series36* of bytes.37* <p>38* For all the methods in this interface that39* write bytes, it is generally true that if40* a byte cannot be written for any reason,41* an <code>IOException</code> is thrown.42*43* @author Frank Yellin44* @see java.io.DataInput45* @see java.io.DataOutputStream46* @since JDK1.047*/48public49interface DataOutput {50/**51* Writes to the output stream the eight52* low-order bits of the argument <code>b</code>.53* The 24 high-order bits of <code>b</code>54* are ignored.55*56* @param b the byte to be written.57* @throws IOException if an I/O error occurs.58*/59void write(int b) throws IOException;6061/**62* Writes to the output stream all the bytes in array <code>b</code>.63* If <code>b</code> is <code>null</code>,64* a <code>NullPointerException</code> is thrown.65* If <code>b.length</code> is zero, then66* no bytes are written. Otherwise, the byte67* <code>b[0]</code> is written first, then68* <code>b[1]</code>, and so on; the last byte69* written is <code>b[b.length-1]</code>.70*71* @param b the data.72* @throws IOException if an I/O error occurs.73*/74void write(byte b[]) throws IOException;7576/**77* Writes <code>len</code> bytes from array78* <code>b</code>, in order, to79* the output stream. If <code>b</code>80* is <code>null</code>, a <code>NullPointerException</code>81* is thrown. If <code>off</code> is negative,82* or <code>len</code> is negative, or <code>off+len</code>83* is greater than the length of the array84* <code>b</code>, then an <code>IndexOutOfBoundsException</code>85* is thrown. If <code>len</code> is zero,86* then no bytes are written. Otherwise, the87* byte <code>b[off]</code> is written first,88* then <code>b[off+1]</code>, and so on; the89* last byte written is <code>b[off+len-1]</code>.90*91* @param b the data.92* @param off the start offset in the data.93* @param len the number of bytes to write.94* @throws IOException if an I/O error occurs.95*/96void write(byte b[], int off, int len) throws IOException;9798/**99* Writes a <code>boolean</code> value to this output stream.100* If the argument <code>v</code>101* is <code>true</code>, the value <code>(byte)1</code>102* is written; if <code>v</code> is <code>false</code>,103* the value <code>(byte)0</code> is written.104* The byte written by this method may105* be read by the <code>readBoolean</code>106* method of interface <code>DataInput</code>,107* which will then return a <code>boolean</code>108* equal to <code>v</code>.109*110* @param v the boolean to be written.111* @throws IOException if an I/O error occurs.112*/113void writeBoolean(boolean v) throws IOException;114115/**116* Writes to the output stream the eight low-117* order bits of the argument <code>v</code>.118* The 24 high-order bits of <code>v</code>119* are ignored. (This means that <code>writeByte</code>120* does exactly the same thing as <code>write</code>121* for an integer argument.) The byte written122* by this method may be read by the <code>readByte</code>123* method of interface <code>DataInput</code>,124* which will then return a <code>byte</code>125* equal to <code>(byte)v</code>.126*127* @param v the byte value to be written.128* @throws IOException if an I/O error occurs.129*/130void writeByte(int v) throws IOException;131132/**133* Writes two bytes to the output134* stream to represent the value of the argument.135* The byte values to be written, in the order136* shown, are:137* <pre>{@code138* (byte)(0xff & (v >> 8))139* (byte)(0xff & v)140* }</pre> <p>141* The bytes written by this method may be142* read by the <code>readShort</code> method143* of interface <code>DataInput</code> , which144* will then return a <code>short</code> equal145* to <code>(short)v</code>.146*147* @param v the <code>short</code> value to be written.148* @throws IOException if an I/O error occurs.149*/150void writeShort(int v) throws IOException;151152/**153* Writes a <code>char</code> value, which154* is comprised of two bytes, to the155* output stream.156* The byte values to be written, in the order157* shown, are:158* <pre>{@code159* (byte)(0xff & (v >> 8))160* (byte)(0xff & v)161* }</pre><p>162* The bytes written by this method may be163* read by the <code>readChar</code> method164* of interface <code>DataInput</code> , which165* will then return a <code>char</code> equal166* to <code>(char)v</code>.167*168* @param v the <code>char</code> value to be written.169* @throws IOException if an I/O error occurs.170*/171void writeChar(int v) throws IOException;172173/**174* Writes an <code>int</code> value, which is175* comprised of four bytes, to the output stream.176* The byte values to be written, in the order177* shown, are:178* <pre>{@code179* (byte)(0xff & (v >> 24))180* (byte)(0xff & (v >> 16))181* (byte)(0xff & (v >> 8))182* (byte)(0xff & v)183* }</pre><p>184* The bytes written by this method may be read185* by the <code>readInt</code> method of interface186* <code>DataInput</code> , which will then187* return an <code>int</code> equal to <code>v</code>.188*189* @param v the <code>int</code> value to be written.190* @throws IOException if an I/O error occurs.191*/192void writeInt(int v) throws IOException;193194/**195* Writes a <code>long</code> value, which is196* comprised of eight bytes, to the output stream.197* The byte values to be written, in the order198* shown, are:199* <pre>{@code200* (byte)(0xff & (v >> 56))201* (byte)(0xff & (v >> 48))202* (byte)(0xff & (v >> 40))203* (byte)(0xff & (v >> 32))204* (byte)(0xff & (v >> 24))205* (byte)(0xff & (v >> 16))206* (byte)(0xff & (v >> 8))207* (byte)(0xff & v)208* }</pre><p>209* The bytes written by this method may be210* read by the <code>readLong</code> method211* of interface <code>DataInput</code> , which212* will then return a <code>long</code> equal213* to <code>v</code>.214*215* @param v the <code>long</code> value to be written.216* @throws IOException if an I/O error occurs.217*/218void writeLong(long v) throws IOException;219220/**221* Writes a <code>float</code> value,222* which is comprised of four bytes, to the output stream.223* It does this as if it first converts this224* <code>float</code> value to an <code>int</code>225* in exactly the manner of the <code>Float.floatToIntBits</code>226* method and then writes the <code>int</code>227* value in exactly the manner of the <code>writeInt</code>228* method. The bytes written by this method229* may be read by the <code>readFloat</code>230* method of interface <code>DataInput</code>,231* which will then return a <code>float</code>232* equal to <code>v</code>.233*234* @param v the <code>float</code> value to be written.235* @throws IOException if an I/O error occurs.236*/237void writeFloat(float v) throws IOException;238239/**240* Writes a <code>double</code> value,241* which is comprised of eight bytes, to the output stream.242* It does this as if it first converts this243* <code>double</code> value to a <code>long</code>244* in exactly the manner of the <code>Double.doubleToLongBits</code>245* method and then writes the <code>long</code>246* value in exactly the manner of the <code>writeLong</code>247* method. The bytes written by this method248* may be read by the <code>readDouble</code>249* method of interface <code>DataInput</code>,250* which will then return a <code>double</code>251* equal to <code>v</code>.252*253* @param v the <code>double</code> value to be written.254* @throws IOException if an I/O error occurs.255*/256void writeDouble(double v) throws IOException;257258/**259* Writes a string to the output stream.260* For every character in the string261* <code>s</code>, taken in order, one byte262* is written to the output stream. If263* <code>s</code> is <code>null</code>, a <code>NullPointerException</code>264* is thrown.<p> If <code>s.length</code>265* is zero, then no bytes are written. Otherwise,266* the character <code>s[0]</code> is written267* first, then <code>s[1]</code>, and so on;268* the last character written is <code>s[s.length-1]</code>.269* For each character, one byte is written,270* the low-order byte, in exactly the manner271* of the <code>writeByte</code> method . The272* high-order eight bits of each character273* in the string are ignored.274*275* @param s the string of bytes to be written.276* @throws IOException if an I/O error occurs.277*/278void writeBytes(String s) throws IOException;279280/**281* Writes every character in the string <code>s</code>,282* to the output stream, in order,283* two bytes per character. If <code>s</code>284* is <code>null</code>, a <code>NullPointerException</code>285* is thrown. If <code>s.length</code>286* is zero, then no characters are written.287* Otherwise, the character <code>s[0]</code>288* is written first, then <code>s[1]</code>,289* and so on; the last character written is290* <code>s[s.length-1]</code>. For each character,291* two bytes are actually written, high-order292* byte first, in exactly the manner of the293* <code>writeChar</code> method.294*295* @param s the string value to be written.296* @throws IOException if an I/O error occurs.297*/298void writeChars(String s) throws IOException;299300/**301* Writes two bytes of length information302* to the output stream, followed303* by the304* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>305* representation306* of every character in the string <code>s</code>.307* If <code>s</code> is <code>null</code>,308* a <code>NullPointerException</code> is thrown.309* Each character in the string <code>s</code>310* is converted to a group of one, two, or311* three bytes, depending on the value of the312* character.<p>313* If a character <code>c</code>314* is in the range <code>\u0001</code> through315* <code>\u007f</code>, it is represented316* by one byte:317* <pre>(byte)c </pre> <p>318* If a character <code>c</code> is <code>\u0000</code>319* or is in the range <code>\u0080</code>320* through <code>\u07ff</code>, then it is321* represented by two bytes, to be written322* in the order shown: <pre>{@code323* (byte)(0xc0 | (0x1f & (c >> 6)))324* (byte)(0x80 | (0x3f & c))325* }</pre> <p> If a character326* <code>c</code> is in the range <code>\u0800</code>327* through <code>uffff</code>, then it is328* represented by three bytes, to be written329* in the order shown: <pre>{@code330* (byte)(0xe0 | (0x0f & (c >> 12)))331* (byte)(0x80 | (0x3f & (c >> 6)))332* (byte)(0x80 | (0x3f & c))333* }</pre> <p> First,334* the total number of bytes needed to represent335* all the characters of <code>s</code> is336* calculated. If this number is larger than337* <code>65535</code>, then a <code>UTFDataFormatException</code>338* is thrown. Otherwise, this length is written339* to the output stream in exactly the manner340* of the <code>writeShort</code> method;341* after this, the one-, two-, or three-byte342* representation of each character in the343* string <code>s</code> is written.<p> The344* bytes written by this method may be read345* by the <code>readUTF</code> method of interface346* <code>DataInput</code> , which will then347* return a <code>String</code> equal to <code>s</code>.348*349* @param s the string value to be written.350* @throws IOException if an I/O error occurs.351*/352void writeUTF(String s) throws IOException;353}354355356