dataoutputstream.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 535 行 · 第 1/2 页
JAVA
535 行
/* DataOutputStream.java -- Writes primitive Java datatypes to streams
Copyright (C) 1998, 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.io;
/**
* This class provides a mechanism for writing primitive Java datatypes
* to an <code>OutputStream</code> in a portable way. Data written to
* a stream using this class can be read back in using the
* <code>DataInputStream</code> class on any platform.
*
* @see DataInputStream
*
* @version 0.0
*
* @author Aaron M. Renn (arenn@urbanophile.com)
*/
public class DataOutputStream extends FilterOutputStream implements DataOutput
{
/*************************************************************************/
/*
* Instance Variables
*/
/**
* This is the total number of bytes that have been written to the
* stream by this object instance.
*/
protected int written;
/*************************************************************************/
/*
* Class Methods
*/
/*
* These converter methods are called by instance methods of this class
* and by instance methods of RandomAccessFile to convert data read. For
* documentation on what these do, please see the corresponding instance
* method documentation
*/
static final int
convertFromBoolean(boolean b)
{
if (b)
return(1);
else
return(0);
}
/*************************************************************************/
static final byte[]
convertFromChar(int c)
{
byte[] buf = new byte[2];
buf[0] = (byte)((c & 0xFF00) >> 8);
buf[1] = (byte)((int)c & 0x00FF);
return(buf);
}
/*************************************************************************/
static final byte[]
getConvertedStringChars(String s)
{
byte[] buf = new byte[s.length() * 2];
for (int i = 0; i < s.length(); i++)
{
buf[i * 2] = (byte)(s.charAt(i) & 0xFF00);
buf[(i * 2) + 1] = (byte)(s.charAt(i) & 0x00FF);
}
return(buf);
}
/*************************************************************************/
static final byte[]
convertFromShort(int s)
{
byte[] buf = new byte[2];
buf[0] = (byte)((s & 0xFF00) >> 8);
buf[1] = (byte)(s & 0x00FF);
return(buf);
}
/*************************************************************************/
static final byte[]
convertFromInt(int i)
{
byte[] buf = new byte[4];
buf[0] = (byte)((i & 0xFF000000) >> 24);
buf[1] = (byte)((i & 0x00FF0000) >> 16);
buf[2] = (byte)((i & 0x0000FF00) >> 8);
buf[3] = (byte)(i & 0x000000FF);
return(buf);
}
/*************************************************************************/
static final byte[]
convertFromLong(long l)
{
byte[] buf = new byte[8];
buf[0] = (byte)((l & 0xFF00000000000000L) >> 56);
buf[1] = (byte)((l & 0x00FF000000000000L) >> 48);
buf[2] = (byte)((l & 0x0000FF0000000000L) >> 40);
buf[3] = (byte)((l & 0x000000FF00000000L) >> 32);
buf[4] = (byte)((l & 0x00000000FF000000L) >> 24);
buf[5] = (byte)((l & 0x0000000000FF0000L) >> 16);
buf[6] = (byte)((l & 0x000000000000FF00L) >> 8);
buf[7] = (byte)(l & 0x00000000000000FFL);
return(buf);
}
/*************************************************************************/
static final byte[]
convertToUTF(String s) throws IOException
{
ByteArrayOutputStream os = new ByteArrayOutputStream(s.length());
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if ((c <= 0x007F) && (c != 0))
{
os.write(c);
}
else if ((c <= 0x07FF) || (c == 0))
{
byte b1 = (byte)(0xC0 | (c >> 6));
byte b2 = (byte)(0x80 | (c & 0x3F));
os.write(b1);
os.write(b2);
}
else
{
byte b1 = (byte)(0xE0 | (c >> 12));
byte b2 = (byte)(0x80 | ((c >> 6) & 0x3F));
byte b3 = (byte)(0x80 | (c & 0x3F));
os.write(b1);
os.write(b2);
os.write(b3);
}
}
return(os.toByteArray());
}
/*************************************************************************/
/*
* Constructors
*/
/**
* This method initializes an instance of <code>DataOutputStream</code> to
* write its data to the specified underlying <code>OutputStream</code>
*
* @param out The subordinate <code>OutputStream</code> to which this object will write
*/
public
DataOutputStream(OutputStream out)
{
super(out);
}
/*************************************************************************/
/*
* Instance Methods
*/
/**
* This method returns the total number of bytes that have been written to
* the underlying output stream so far. This is the value of the
* <code>written</code> instance variable
*
* @return The number of bytes written to the stream.
*/
public final int
size()
{
return(written);
}
/*************************************************************************/
/**
* This method writes a Java <code>boolean</code> to the underlying output
* stream. For a value of <code>true</code>, 1 is written to the stream.
* For a value of <code>false</code>, 0 is written.
*
* @param b The <code>boolean</code> value to write to the stream
*
* @exception IOException If an error occurs
*/
public final void
writeBoolean(boolean b) throws IOException
{
int bool = convertFromBoolean(b);
out.write(bool);
++written;
}
/*************************************************************************/
/**
* This method writes a Java <code>byte</code> value to the underlying
* output stream.
*
* @param b The <code>byte</code> to write to the stream, passed as an <code>int</code>.
*
* @exception IOException If an error occurs
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?