Path: blob/master/src/java.base/share/classes/java/io/DataOutput.java
41152 views
/*1* Copyright (c) 1995, 2020, 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} 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} 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} is thrown.42*43* @author Frank Yellin44* @see java.io.DataInput45* @see java.io.DataOutputStream46* @since 1.047*/48public interface DataOutput {49/**50* Writes to the output stream the eight51* low-order bits of the argument {@code b}.52* The 24 high-order bits of {@code b}53* are ignored.54*55* @param b the byte to be written.56* @throws IOException if an I/O error occurs.57*/58void write(int b) throws IOException;5960/**61* Writes to the output stream all the bytes in array {@code b}.62* If {@code b} is {@code null},63* a {@code NullPointerException} is thrown.64* If {@code b.length} is zero, then65* no bytes are written. Otherwise, the byte66* {@code b[0]} is written first, then67* {@code b[1]}, and so on; the last byte68* written is {@code b[b.length-1]}.69*70* @param b the data.71* @throws IOException if an I/O error occurs.72*/73void write(byte b[]) throws IOException;7475/**76* Writes {@code len} bytes from array77* {@code b}, in order, to78* the output stream. If {@code b}79* is {@code null}, a {@code NullPointerException}80* is thrown. If {@code off} is negative,81* or {@code len} is negative, or {@code off+len}82* is greater than the length of the array83* {@code b}, then an {@code IndexOutOfBoundsException}84* is thrown. If {@code len} is zero,85* then no bytes are written. Otherwise, the86* byte {@code b[off]} is written first,87* then {@code b[off+1]}, and so on; the88* last byte written is {@code b[off+len-1]}.89*90* @param b the data.91* @param off the start offset in the data.92* @param len the number of bytes to write.93* @throws IOException if an I/O error occurs.94*/95void write(byte b[], int off, int len) throws IOException;9697/**98* Writes a {@code boolean} value to this output stream.99* If the argument {@code v}100* is {@code true}, the value {@code (byte)1}101* is written; if {@code v} is {@code false},102* the value {@code (byte)0} is written.103* The byte written by this method may104* be read by the {@code readBoolean}105* method of interface {@code DataInput},106* which will then return a {@code boolean}107* equal to {@code v}.108*109* @param v the boolean to be written.110* @throws IOException if an I/O error occurs.111*/112void writeBoolean(boolean v) throws IOException;113114/**115* Writes to the output stream the eight low-order116* bits of the argument {@code v}.117* The 24 high-order bits of {@code v}118* are ignored. (This means that {@code writeByte}119* does exactly the same thing as {@code write}120* for an integer argument.) The byte written121* by this method may be read by the {@code readByte}122* method of interface {@code DataInput},123* which will then return a {@code byte}124* equal to {@code (byte)v}.125*126* @param v the byte value to be written.127* @throws IOException if an I/O error occurs.128*/129void writeByte(int v) throws IOException;130131/**132* Writes two bytes to the output133* stream to represent the value of the argument.134* The byte values to be written, in the order135* shown, are:136* <pre>{@code137* (byte)(0xff & (v >> 8))138* (byte)(0xff & v)139* }</pre> <p>140* The bytes written by this method may be141* read by the {@code readShort} method142* of interface {@code DataInput}, which143* will then return a {@code short} equal144* to {@code (short)v}.145*146* @param v the {@code short} value to be written.147* @throws IOException if an I/O error occurs.148*/149void writeShort(int v) throws IOException;150151/**152* Writes a {@code char} value, which153* is comprised of two bytes, to the154* output stream.155* The byte values to be written, in the order156* shown, are:157* <pre>{@code158* (byte)(0xff & (v >> 8))159* (byte)(0xff & v)160* }</pre><p>161* The bytes written by this method may be162* read by the {@code readChar} method163* of interface {@code DataInput}, which164* will then return a {@code char} equal165* to {@code (char)v}.166*167* @param v the {@code char} value to be written.168* @throws IOException if an I/O error occurs.169*/170void writeChar(int v) throws IOException;171172/**173* Writes an {@code int} value, which is174* comprised of four bytes, to the output stream.175* The byte values to be written, in the order176* shown, are:177* <pre>{@code178* (byte)(0xff & (v >> 24))179* (byte)(0xff & (v >> 16))180* (byte)(0xff & (v >> 8))181* (byte)(0xff & v)182* }</pre><p>183* The bytes written by this method may be read184* by the {@code readInt} method of interface185* {@code DataInput}, which will then186* return an {@code int} equal to {@code v}.187*188* @param v the {@code int} value to be written.189* @throws IOException if an I/O error occurs.190*/191void writeInt(int v) throws IOException;192193/**194* Writes a {@code long} value, which is195* comprised of eight bytes, to the output stream.196* The byte values to be written, in the order197* shown, are:198* <pre>{@code199* (byte)(0xff & (v >> 56))200* (byte)(0xff & (v >> 48))201* (byte)(0xff & (v >> 40))202* (byte)(0xff & (v >> 32))203* (byte)(0xff & (v >> 24))204* (byte)(0xff & (v >> 16))205* (byte)(0xff & (v >> 8))206* (byte)(0xff & v)207* }</pre><p>208* The bytes written by this method may be209* read by the {@code readLong} method210* of interface {@code DataInput}, which211* will then return a {@code long} equal212* to {@code v}.213*214* @param v the {@code long} value to be written.215* @throws IOException if an I/O error occurs.216*/217void writeLong(long v) throws IOException;218219/**220* Writes a {@code float} value,221* which is comprised of four bytes, to the output stream.222* It does this as if it first converts this223* {@code float} value to an {@code int}224* in exactly the manner of the {@code Float.floatToIntBits}225* method and then writes the {@code int}226* value in exactly the manner of the {@code writeInt}227* method. The bytes written by this method228* may be read by the {@code readFloat}229* method of interface {@code DataInput},230* which will then return a {@code float}231* equal to {@code v}.232*233* @param v the {@code float} value to be written.234* @throws IOException if an I/O error occurs.235*/236void writeFloat(float v) throws IOException;237238/**239* Writes a {@code double} value,240* which is comprised of eight bytes, to the output stream.241* It does this as if it first converts this242* {@code double} value to a {@code long}243* in exactly the manner of the {@code Double.doubleToLongBits}244* method and then writes the {@code long}245* value in exactly the manner of the {@code writeLong}246* method. The bytes written by this method247* may be read by the {@code readDouble}248* method of interface {@code DataInput},249* which will then return a {@code double}250* equal to {@code v}.251*252* @param v the {@code double} value to be written.253* @throws IOException if an I/O error occurs.254*/255void writeDouble(double v) throws IOException;256257/**258* Writes a string to the output stream.259* For every character in the string260* {@code s}, taken in order, one byte261* is written to the output stream. If262* {@code s} is {@code null}, a {@code NullPointerException}263* is thrown.<p> If {@code s.length}264* is zero, then no bytes are written. Otherwise,265* the character {@code s[0]} is written266* first, then {@code s[1]}, and so on;267* the last character written is {@code s[s.length-1]}.268* For each character, one byte is written,269* the low-order byte, in exactly the manner270* of the {@code writeByte} method . The271* high-order eight bits of each character272* in the string are ignored.273*274* @param s the string of bytes to be written.275* @throws IOException if an I/O error occurs.276*/277void writeBytes(String s) throws IOException;278279/**280* Writes every character in the string {@code s},281* to the output stream, in order,282* two bytes per character. If {@code s}283* is {@code null}, a {@code NullPointerException}284* is thrown. If {@code s.length}285* is zero, then no characters are written.286* Otherwise, the character {@code s[0]}287* is written first, then {@code s[1]},288* and so on; the last character written is289* {@code s[s.length-1]}. For each character,290* two bytes are actually written, high-order291* byte first, in exactly the manner of the292* {@code writeChar} method.293*294* @param s the string value to be written.295* @throws IOException if an I/O error occurs.296*/297void writeChars(String s) throws IOException;298299/**300* Writes two bytes of length information301* to the output stream, followed302* by the303* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>304* representation305* of every character in the string {@code s}.306* If {@code s} is {@code null},307* a {@code NullPointerException} is thrown.308* Each character in the string {@code s}309* is converted to a group of one, two, or310* three bytes, depending on the value of the311* character.<p>312* If a character {@code c}313* is in the range <code>\u0001</code> through314* <code>\u007f</code>, it is represented315* by one byte:316* <pre>(byte)c </pre> <p>317* If a character {@code c} is <code>\u0000</code>318* or is in the range <code>\u0080</code>319* through <code>\u07ff</code>, then it is320* represented by two bytes, to be written321* in the order shown: <pre>{@code322* (byte)(0xc0 | (0x1f & (c >> 6)))323* (byte)(0x80 | (0x3f & c))324* }</pre> <p> If a character325* {@code c} is in the range <code>\u0800</code>326* through {@code uffff}, then it is327* represented by three bytes, to be written328* in the order shown: <pre>{@code329* (byte)(0xe0 | (0x0f & (c >> 12)))330* (byte)(0x80 | (0x3f & (c >> 6)))331* (byte)(0x80 | (0x3f & c))332* }</pre> <p> First,333* the total number of bytes needed to represent334* all the characters of {@code s} is335* calculated. If this number is larger than336* {@code 65535}, then a {@code UTFDataFormatException}337* is thrown. Otherwise, this length is written338* to the output stream in exactly the manner339* of the {@code writeShort} method;340* after this, the one-, two-, or three-byte341* representation of each character in the342* string {@code s} is written.<p> The343* bytes written by this method may be read344* by the {@code readUTF} method of interface345* {@code DataInput}, which will then346* return a {@code String} equal to {@code s}.347*348* @param s the string value to be written.349* @throws IOException if an I/O error occurs.350*/351void writeUTF(String s) throws IOException;352}353354355