Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/DataOutputStream.java
38829 views
/*1* Copyright (c) 1994, 2004, 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* A data output stream lets an application write primitive Java data29* types to an output stream in a portable way. An application can30* then use a data input stream to read the data back in.31*32* @author unascribed33* @see java.io.DataInputStream34* @since JDK1.035*/36public37class DataOutputStream extends FilterOutputStream implements DataOutput {38/**39* The number of bytes written to the data output stream so far.40* If this counter overflows, it will be wrapped to Integer.MAX_VALUE.41*/42protected int written;4344/**45* bytearr is initialized on demand by writeUTF46*/47private byte[] bytearr = null;4849/**50* Creates a new data output stream to write data to the specified51* underlying output stream. The counter <code>written</code> is52* set to zero.53*54* @param out the underlying output stream, to be saved for later55* use.56* @see java.io.FilterOutputStream#out57*/58public DataOutputStream(OutputStream out) {59super(out);60}6162/**63* Increases the written counter by the specified value64* until it reaches Integer.MAX_VALUE.65*/66private void incCount(int value) {67int temp = written + value;68if (temp < 0) {69temp = Integer.MAX_VALUE;70}71written = temp;72}7374/**75* Writes the specified byte (the low eight bits of the argument76* <code>b</code>) to the underlying output stream. If no exception77* is thrown, the counter <code>written</code> is incremented by78* <code>1</code>.79* <p>80* Implements the <code>write</code> method of <code>OutputStream</code>.81*82* @param b the <code>byte</code> to be written.83* @exception IOException if an I/O error occurs.84* @see java.io.FilterOutputStream#out85*/86public synchronized void write(int b) throws IOException {87out.write(b);88incCount(1);89}9091/**92* Writes <code>len</code> bytes from the specified byte array93* starting at offset <code>off</code> to the underlying output stream.94* If no exception is thrown, the counter <code>written</code> is95* incremented by <code>len</code>.96*97* @param b the data.98* @param off the start offset in the data.99* @param len the number of bytes to write.100* @exception IOException if an I/O error occurs.101* @see java.io.FilterOutputStream#out102*/103public synchronized void write(byte b[], int off, int len)104throws IOException105{106out.write(b, off, len);107incCount(len);108}109110/**111* Flushes this data output stream. This forces any buffered output112* bytes to be written out to the stream.113* <p>114* The <code>flush</code> method of <code>DataOutputStream</code>115* calls the <code>flush</code> method of its underlying output stream.116*117* @exception IOException if an I/O error occurs.118* @see java.io.FilterOutputStream#out119* @see java.io.OutputStream#flush()120*/121public void flush() throws IOException {122out.flush();123}124125/**126* Writes a <code>boolean</code> to the underlying output stream as127* a 1-byte value. The value <code>true</code> is written out as the128* value <code>(byte)1</code>; the value <code>false</code> is129* written out as the value <code>(byte)0</code>. If no exception is130* thrown, the counter <code>written</code> is incremented by131* <code>1</code>.132*133* @param v a <code>boolean</code> value to be written.134* @exception IOException if an I/O error occurs.135* @see java.io.FilterOutputStream#out136*/137public final void writeBoolean(boolean v) throws IOException {138out.write(v ? 1 : 0);139incCount(1);140}141142/**143* Writes out a <code>byte</code> to the underlying output stream as144* a 1-byte value. If no exception is thrown, the counter145* <code>written</code> is incremented by <code>1</code>.146*147* @param v a <code>byte</code> value to be written.148* @exception IOException if an I/O error occurs.149* @see java.io.FilterOutputStream#out150*/151public final void writeByte(int v) throws IOException {152out.write(v);153incCount(1);154}155156/**157* Writes a <code>short</code> to the underlying output stream as two158* bytes, high byte first. If no exception is thrown, the counter159* <code>written</code> is incremented by <code>2</code>.160*161* @param v a <code>short</code> to be written.162* @exception IOException if an I/O error occurs.163* @see java.io.FilterOutputStream#out164*/165public final void writeShort(int v) throws IOException {166out.write((v >>> 8) & 0xFF);167out.write((v >>> 0) & 0xFF);168incCount(2);169}170171/**172* Writes a <code>char</code> to the underlying output stream as a173* 2-byte value, high byte first. If no exception is thrown, the174* counter <code>written</code> is incremented by <code>2</code>.175*176* @param v a <code>char</code> value to be written.177* @exception IOException if an I/O error occurs.178* @see java.io.FilterOutputStream#out179*/180public final void writeChar(int v) throws IOException {181out.write((v >>> 8) & 0xFF);182out.write((v >>> 0) & 0xFF);183incCount(2);184}185186/**187* Writes an <code>int</code> to the underlying output stream as four188* bytes, high byte first. If no exception is thrown, the counter189* <code>written</code> is incremented by <code>4</code>.190*191* @param v an <code>int</code> to be written.192* @exception IOException if an I/O error occurs.193* @see java.io.FilterOutputStream#out194*/195public final void writeInt(int v) throws IOException {196out.write((v >>> 24) & 0xFF);197out.write((v >>> 16) & 0xFF);198out.write((v >>> 8) & 0xFF);199out.write((v >>> 0) & 0xFF);200incCount(4);201}202203private byte writeBuffer[] = new byte[8];204205/**206* Writes a <code>long</code> to the underlying output stream as eight207* bytes, high byte first. In no exception is thrown, the counter208* <code>written</code> is incremented by <code>8</code>.209*210* @param v a <code>long</code> to be written.211* @exception IOException if an I/O error occurs.212* @see java.io.FilterOutputStream#out213*/214public final void writeLong(long v) throws IOException {215writeBuffer[0] = (byte)(v >>> 56);216writeBuffer[1] = (byte)(v >>> 48);217writeBuffer[2] = (byte)(v >>> 40);218writeBuffer[3] = (byte)(v >>> 32);219writeBuffer[4] = (byte)(v >>> 24);220writeBuffer[5] = (byte)(v >>> 16);221writeBuffer[6] = (byte)(v >>> 8);222writeBuffer[7] = (byte)(v >>> 0);223out.write(writeBuffer, 0, 8);224incCount(8);225}226227/**228* Converts the float argument to an <code>int</code> using the229* <code>floatToIntBits</code> method in class <code>Float</code>,230* and then writes that <code>int</code> value to the underlying231* output stream as a 4-byte quantity, high byte first. If no232* exception is thrown, the counter <code>written</code> is233* incremented by <code>4</code>.234*235* @param v a <code>float</code> value to be written.236* @exception IOException if an I/O error occurs.237* @see java.io.FilterOutputStream#out238* @see java.lang.Float#floatToIntBits(float)239*/240public final void writeFloat(float v) throws IOException {241writeInt(Float.floatToIntBits(v));242}243244/**245* Converts the double argument to a <code>long</code> using the246* <code>doubleToLongBits</code> method in class <code>Double</code>,247* and then writes that <code>long</code> value to the underlying248* output stream as an 8-byte quantity, high byte first. If no249* exception is thrown, the counter <code>written</code> is250* incremented by <code>8</code>.251*252* @param v a <code>double</code> value to be written.253* @exception IOException if an I/O error occurs.254* @see java.io.FilterOutputStream#out255* @see java.lang.Double#doubleToLongBits(double)256*/257public final void writeDouble(double v) throws IOException {258writeLong(Double.doubleToLongBits(v));259}260261/**262* Writes out the string to the underlying output stream as a263* sequence of bytes. Each character in the string is written out, in264* sequence, by discarding its high eight bits. If no exception is265* thrown, the counter <code>written</code> is incremented by the266* length of <code>s</code>.267*268* @param s a string of bytes to be written.269* @exception IOException if an I/O error occurs.270* @see java.io.FilterOutputStream#out271*/272public final void writeBytes(String s) throws IOException {273int len = s.length();274for (int i = 0 ; i < len ; i++) {275out.write((byte)s.charAt(i));276}277incCount(len);278}279280/**281* Writes a string to the underlying output stream as a sequence of282* characters. Each character is written to the data output stream as283* if by the <code>writeChar</code> method. If no exception is284* thrown, the counter <code>written</code> is incremented by twice285* the length of <code>s</code>.286*287* @param s a <code>String</code> value to be written.288* @exception IOException if an I/O error occurs.289* @see java.io.DataOutputStream#writeChar(int)290* @see java.io.FilterOutputStream#out291*/292public final void writeChars(String s) throws IOException {293int len = s.length();294for (int i = 0 ; i < len ; i++) {295int v = s.charAt(i);296out.write((v >>> 8) & 0xFF);297out.write((v >>> 0) & 0xFF);298}299incCount(len * 2);300}301302/**303* Writes a string to the underlying output stream using304* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>305* encoding in a machine-independent manner.306* <p>307* First, two bytes are written to the output stream as if by the308* <code>writeShort</code> method giving the number of bytes to309* follow. This value is the number of bytes actually written out,310* not the length of the string. Following the length, each character311* of the string is output, in sequence, using the modified UTF-8 encoding312* for the character. If no exception is thrown, the counter313* <code>written</code> is incremented by the total number of314* bytes written to the output stream. This will be at least two315* plus the length of <code>str</code>, and at most two plus316* thrice the length of <code>str</code>.317*318* @param str a string to be written.319* @exception IOException if an I/O error occurs.320*/321public final void writeUTF(String str) throws IOException {322writeUTF(str, this);323}324325/**326* Writes a string to the specified DataOutput using327* <a href="DataInput.html#modified-utf-8">modified UTF-8</a>328* encoding in a machine-independent manner.329* <p>330* First, two bytes are written to out as if by the <code>writeShort</code>331* method giving the number of bytes to follow. This value is the number of332* bytes actually written out, not the length of the string. Following the333* length, each character of the string is output, in sequence, using the334* modified UTF-8 encoding for the character. If no exception is thrown, the335* counter <code>written</code> is incremented by the total number of336* bytes written to the output stream. This will be at least two337* plus the length of <code>str</code>, and at most two plus338* thrice the length of <code>str</code>.339*340* @param str a string to be written.341* @param out destination to write to342* @return The number of bytes written out.343* @exception IOException if an I/O error occurs.344*/345static int writeUTF(String str, DataOutput out) throws IOException {346int strlen = str.length();347int utflen = 0;348int c, count = 0;349350/* use charAt instead of copying String to char array */351for (int i = 0; i < strlen; i++) {352c = str.charAt(i);353if ((c >= 0x0001) && (c <= 0x007F)) {354utflen++;355} else if (c > 0x07FF) {356utflen += 3;357} else {358utflen += 2;359}360}361362if (utflen > 65535)363throw new UTFDataFormatException(364"encoded string too long: " + utflen + " bytes");365366byte[] bytearr = null;367if (out instanceof DataOutputStream) {368DataOutputStream dos = (DataOutputStream)out;369if(dos.bytearr == null || (dos.bytearr.length < (utflen+2)))370dos.bytearr = new byte[(utflen*2) + 2];371bytearr = dos.bytearr;372} else {373bytearr = new byte[utflen+2];374}375376bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);377bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);378379int i=0;380for (i=0; i<strlen; i++) {381c = str.charAt(i);382if (!((c >= 0x0001) && (c <= 0x007F))) break;383bytearr[count++] = (byte) c;384}385386for (;i < strlen; i++){387c = str.charAt(i);388if ((c >= 0x0001) && (c <= 0x007F)) {389bytearr[count++] = (byte) c;390391} else if (c > 0x07FF) {392bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));393bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));394bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));395} else {396bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));397bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));398}399}400out.write(bytearr, 0, utflen+2);401return utflen + 2;402}403404/**405* Returns the current value of the counter <code>written</code>,406* the number of bytes written to this data output stream so far.407* If the counter overflows, it will be wrapped to Integer.MAX_VALUE.408*409* @return the value of the <code>written</code> field.410* @see java.io.DataOutputStream#written411*/412public final int size() {413return written;414}415}416417418