mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Added some missing classes to be able to compile codenameone.
This commit is contained in:
parent
cff91e03ff
commit
73a1e95aae
|
@ -0,0 +1,113 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation. Codename One designates this
|
||||||
|
* particular file as subject to the "Classpath" exception as provided
|
||||||
|
* by Oracle in the LICENSE file that accompanied this code.
|
||||||
|
*
|
||||||
|
* This code 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
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Codename One through http://www.codenameone.com/ if you
|
||||||
|
* need additional information or have any questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.teavm.classlib.java.io;
|
||||||
|
import org.teavm.classlib.java.lang.*;
|
||||||
|
/**
|
||||||
|
* The DataOutput interface provides for converting data from any of the Java primitive types to a series of bytes and writing these bytes to a binary stream. There is also a facility for converting a String into Java modified UTF-8 format and writing the resulting series of bytes.
|
||||||
|
* For all the methods in this interface that write bytes, it is generally true that if a byte cannot be written for any reason, an IOException is thrown.
|
||||||
|
* Since: JDK1.0, CLDC 1.0 See Also:DataInput, DataOutputStream
|
||||||
|
*/
|
||||||
|
public interface TDataOutput{
|
||||||
|
/**
|
||||||
|
* Writes to the output stream all the bytes in array b. If b is null, a NullPointerException is thrown. If b.length is zero, then no bytes are written. Otherwise, the byte b[0] is written first, then b[1], and so on; the last byte written is b[b.length-1].
|
||||||
|
*/
|
||||||
|
public abstract void write(byte[] b) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes len bytes from array b, in order, to the output stream. If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. If len is zero, then no bytes are written. Otherwise, the byte b[off] is written first, then b[off+1], and so on; the last byte written is b[off+len-1].
|
||||||
|
*/
|
||||||
|
public abstract void write(byte[] b, int off, int len) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes to the output stream the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.
|
||||||
|
*/
|
||||||
|
public abstract void write(int b) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a boolean value to this output stream. If the argument v is true, the value (byte)1 is written; if v is false, the value (byte)0 is written. The byte written by this method may be read by the readBoolean method of interface DataInput, which will then return a boolean equal to v.
|
||||||
|
*/
|
||||||
|
public abstract void writeBoolean(boolean v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes to the output stream the eight low- order bits of the argument v. The 24 high-order bits of v are ignored. (This means that writeByte does exactly the same thing as write for an integer argument.) The byte written by this method may be read by the readByte method of interface DataInput, which will then return a byte equal to (byte)v.
|
||||||
|
*/
|
||||||
|
public abstract void writeByte(int v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a char value, which is comprised of two bytes, to the output stream. The byte values to be written, in the order shown, are:
|
||||||
|
* (byte)(0xff (v 8)) (byte)(0xff v)
|
||||||
|
* The bytes written by this method may be read by the readChar method of interface DataInput, which will then return a char equal to (char)v.
|
||||||
|
*/
|
||||||
|
public abstract void writeChar(int v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes every character in the string s, to the output stream, in order, two bytes per character. If s is null, a NullPointerException is thrown. If s.length is zero, then no characters are written. Otherwise, the character s[0] is written first, then s[1], and so on; the last character written is s[s.length-1]. For each character, two bytes are actually written, high-order byte first, in exactly the manner of the writeChar method.
|
||||||
|
*/
|
||||||
|
public abstract void writeChars(TString s) throws java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a double value, which is comprised of eight bytes, to the output stream. It does this as if it first converts this double value to a long in exactly the manner of the Double.doubleToLongBits method and then writes the long value in exactly the manner of the writeLong method. The bytes written by this method may be read by the readDouble method of interface DataInput, which will then return a double equal to v.
|
||||||
|
*/
|
||||||
|
public abstract void writeDouble(double v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a float value, which is comprised of four bytes, to the output stream. It does this as if it first converts this float value to an int in exactly the manner of the Float.floatToIntBits method and then writes the int value in exactly the manner of the writeInt method. The bytes written by this method may be read by the readFloat method of interface DataInput, which will then return a float equal to v.
|
||||||
|
*/
|
||||||
|
public abstract void writeFloat(float v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes an int value, which is comprised of four bytes, to the output stream. The byte values to be written, in the order shown, are:
|
||||||
|
* (byte)(0xff (v 24)) (byte)(0xff (v 16)) (byte)(0xff (v 8)) (byte)(0xff v)
|
||||||
|
* The bytes written by this method may be read by the readInt method of interface DataInput, which will then return an int equal to v.
|
||||||
|
*/
|
||||||
|
public abstract void writeInt(int v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes an long value, which is comprised of four bytes, to the output stream. The byte values to be written, in the order shown, are:
|
||||||
|
* (byte)(0xff (v 56)) (byte)(0xff (v 48)) (byte)(0xff (v 40)) (byte)(0xff (v 32)) (byte)(0xff (v 24)) (byte)(0xff (v 16)) (byte)(0xff (v 8)) (byte)(0xff v)
|
||||||
|
* The bytes written by this method may be read by the readLong method of interface DataInput, which will then return a long equal to v.
|
||||||
|
*/
|
||||||
|
public abstract void writeLong(long v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes two bytes to the output stream to represent the value of the argument. The byte values to be written, in the order shown, are:
|
||||||
|
* (byte)(0xff (v 8)) (byte)(0xff v)
|
||||||
|
* The bytes written by this method may be read by the readShort method of interface DataInput, which will then return a short equal to (short)v.
|
||||||
|
*/
|
||||||
|
public abstract void writeShort(int v) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes two bytes of length information to the output stream, followed by the Java modified UTF representation of every character in the string s. If s is null, a NullPointerException is thrown. Each character in the string s is converted to a group of one, two, or three bytes, depending on the value of the character.
|
||||||
|
* If a character c is in the range u0001 through u007f, it is represented by one byte:
|
||||||
|
* (byte)c
|
||||||
|
* If a character c is u0000 or is in the range u0080 through u07ff, then it is represented by two bytes, to be written in the order shown:
|
||||||
|
* (byte)(0xc0 | (0x1f (c 6))) (byte)(0x80 | (0x3f c))
|
||||||
|
* If a character c is in the range u0800 through uffff, then it is represented by three bytes, to be written in the order shown:
|
||||||
|
* (byte)(0xe0 | (0x0f (c 12))) (byte)(0x80 | (0x3f (c 6))) (byte)(0x80 | (0x3f c))
|
||||||
|
* First, the total number of bytes needed to represent all the characters of s is calculated. If this number is larger than 65535, then a UTFDataFormatError is thrown. Otherwise, this length is written to the output stream in exactly the manner of the writeShort method; after this, the one-, two-, or three-byte representation of each character in the string s is written.
|
||||||
|
* The bytes written by this method may be read by the readUTF method of interface DataInput, which will then return a String equal to s.
|
||||||
|
*/
|
||||||
|
public abstract void writeUTF(TString s) throws TIOException;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,371 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.teavm.classlib.java.io;
|
||||||
|
import org.teavm.classlib.java.lang.*;
|
||||||
|
/**
|
||||||
|
* A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.
|
||||||
|
* Since: JDK1.0, CLDC 1.0 See Also:DataInputStream
|
||||||
|
*/
|
||||||
|
public class TDataOutputStream extends TFilterOutputStream implements TDataOutput{
|
||||||
|
/**
|
||||||
|
* The number of bytes written out so far.
|
||||||
|
*/
|
||||||
|
protected int written;
|
||||||
|
byte buff[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new {@code DataOutputStream} on the {@code OutputStream}
|
||||||
|
* {@code out}. Note that data written by this stream is not in a human
|
||||||
|
* readable form but can be reconstructed by using a {@link DataInputStream}
|
||||||
|
* on the resulting output.
|
||||||
|
*
|
||||||
|
* @param out
|
||||||
|
* the target stream for writing.
|
||||||
|
*/
|
||||||
|
public TDataOutputStream(TOutputStream out) {
|
||||||
|
super(out);
|
||||||
|
buff = new byte[8];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flushes this stream to ensure all pending data is sent out to the target
|
||||||
|
* stream. This implementation then also flushes the target stream.
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs attempting to flush this stream.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void flush() throws TIOException {
|
||||||
|
super.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the total number of bytes written to the target stream so far.
|
||||||
|
*
|
||||||
|
* @return the number of bytes written to the target stream.
|
||||||
|
*/
|
||||||
|
public final int size() {
|
||||||
|
if (written < 0) {
|
||||||
|
written = TInteger.MAX_VALUE;
|
||||||
|
}
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes {@code count} bytes from the byte array {@code buffer} starting at
|
||||||
|
* {@code offset} to the target stream.
|
||||||
|
*
|
||||||
|
* @param buffer
|
||||||
|
* the buffer to write to the target stream.
|
||||||
|
* @param offset
|
||||||
|
* the index of the first byte in {@code buffer} to write.
|
||||||
|
* @param count
|
||||||
|
* the number of bytes from the {@code buffer} to write.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @throws NullPointerException
|
||||||
|
* if {@code buffer} is {@code null}.
|
||||||
|
* @see DataInputStream#readFully(byte[])
|
||||||
|
* @see DataInputStream#readFully(byte[], int, int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void write(byte buffer[], int offset, int count) throws TIOException {
|
||||||
|
if (buffer == null) {
|
||||||
|
throw new TNullPointerException();
|
||||||
|
}
|
||||||
|
out.write(buffer, offset, count);
|
||||||
|
written += count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a byte to the target stream. Only the least significant byte of
|
||||||
|
* the integer {@code oneByte} is written.
|
||||||
|
*
|
||||||
|
* @param oneByte
|
||||||
|
* the byte to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readByte()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void write(int oneByte) throws TIOException {
|
||||||
|
out.write(oneByte);
|
||||||
|
written++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a boolean to the target stream.
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the boolean value to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readBoolean()
|
||||||
|
*/
|
||||||
|
public final void writeBoolean(boolean val) throws TIOException {
|
||||||
|
out.write(val ? 1 : 0);
|
||||||
|
written++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes an 8-bit byte to the target stream. Only the least significant
|
||||||
|
* byte of the integer {@code val} is written.
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the byte value to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readByte()
|
||||||
|
* @see DataInputStream#readUnsignedByte()
|
||||||
|
*/
|
||||||
|
public final void writeByte(int val) throws TIOException {
|
||||||
|
out.write(val);
|
||||||
|
written++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the low order bytes from a string to the target stream.
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* the string containing the bytes to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readFully(byte[])
|
||||||
|
* @see DataInputStream#readFully(byte[],int,int)
|
||||||
|
*/
|
||||||
|
public final void writeBytes(TString str) throws TIOException {
|
||||||
|
if (str.length() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
byte bytes[] = new byte[str.length()];
|
||||||
|
for (int index = 0; index < str.length(); index++) {
|
||||||
|
bytes[index] = (byte) str.charAt(index);
|
||||||
|
}
|
||||||
|
out.write(bytes);
|
||||||
|
written += bytes.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a 16-bit character to the target stream. Only the two lower bytes
|
||||||
|
* of the integer {@code val} are written, with the higher one written
|
||||||
|
* first. This corresponds to the Unicode value of {@code val}.
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the character to write to the target stream
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readChar()
|
||||||
|
*/
|
||||||
|
public final void writeChar(int val) throws TIOException {
|
||||||
|
buff[0] = (byte) (val >> 8);
|
||||||
|
buff[1] = (byte) val;
|
||||||
|
out.write(buff, 0, 2);
|
||||||
|
written += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the 16-bit characters contained in {@code str} to the target
|
||||||
|
* stream.
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* the string that contains the characters to write to this
|
||||||
|
* stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readChar()
|
||||||
|
*/
|
||||||
|
public final void writeChars(TString str) throws TIOException {
|
||||||
|
byte newBytes[] = new byte[str.length() * 2];
|
||||||
|
for (int index = 0; index < str.length(); index++) {
|
||||||
|
int newIndex = index == 0 ? index : index * 2;
|
||||||
|
newBytes[newIndex] = (byte) (str.charAt(index) >> 8);
|
||||||
|
newBytes[newIndex + 1] = (byte) str.charAt(index);
|
||||||
|
}
|
||||||
|
out.write(newBytes);
|
||||||
|
written += newBytes.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a 64-bit double to the target stream. The resulting output is the
|
||||||
|
* eight bytes resulting from calling Double.doubleToLongBits().
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the double to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readDouble()
|
||||||
|
*/
|
||||||
|
public final void writeDouble(double val) throws TIOException {
|
||||||
|
writeLong(TDouble.doubleToLongBits(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a 32-bit float to the target stream. The resulting output is the
|
||||||
|
* four bytes resulting from calling Float.floatToIntBits().
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the float to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readFloat()
|
||||||
|
*/
|
||||||
|
public final void writeFloat(float val) throws TIOException {
|
||||||
|
writeInt(TFloat.floatToIntBits(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a 32-bit int to the target stream. The resulting output is the
|
||||||
|
* four bytes, highest order first, of {@code val}.
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the int to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readInt()
|
||||||
|
*/
|
||||||
|
public final void writeInt(int val) throws TIOException {
|
||||||
|
buff[0] = (byte) (val >> 24);
|
||||||
|
buff[1] = (byte) (val >> 16);
|
||||||
|
buff[2] = (byte) (val >> 8);
|
||||||
|
buff[3] = (byte) val;
|
||||||
|
out.write(buff, 0, 4);
|
||||||
|
written += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a 64-bit long to the target stream. The resulting output is the
|
||||||
|
* eight bytes, highest order first, of {@code val}.
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the long to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readLong()
|
||||||
|
*/
|
||||||
|
public final void writeLong(long val) throws TIOException {
|
||||||
|
buff[0] = (byte) (val >> 56);
|
||||||
|
buff[1] = (byte) (val >> 48);
|
||||||
|
buff[2] = (byte) (val >> 40);
|
||||||
|
buff[3] = (byte) (val >> 32);
|
||||||
|
buff[4] = (byte) (val >> 24);
|
||||||
|
buff[5] = (byte) (val >> 16);
|
||||||
|
buff[6] = (byte) (val >> 8);
|
||||||
|
buff[7] = (byte) val;
|
||||||
|
out.write(buff, 0, 8);
|
||||||
|
written += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
int writeLongToBuffer(long val,
|
||||||
|
byte[] buffer, int offset) throws TIOException {
|
||||||
|
buffer[offset++] = (byte) (val >> 56);
|
||||||
|
buffer[offset++] = (byte) (val >> 48);
|
||||||
|
buffer[offset++] = (byte) (val >> 40);
|
||||||
|
buffer[offset++] = (byte) (val >> 32);
|
||||||
|
buffer[offset++] = (byte) (val >> 24);
|
||||||
|
buffer[offset++] = (byte) (val >> 16);
|
||||||
|
buffer[offset++] = (byte) (val >> 8);
|
||||||
|
buffer[offset++] = (byte) val;
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the specified 16-bit short to the target stream. Only the lower
|
||||||
|
* two bytes of the integer {@code val} are written, with the higher one
|
||||||
|
* written first.
|
||||||
|
*
|
||||||
|
* @param val
|
||||||
|
* the short to write to the target stream.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @see DataInputStream#readShort()
|
||||||
|
* @see DataInputStream#readUnsignedShort()
|
||||||
|
*/
|
||||||
|
public final void writeShort(int val) throws TIOException {
|
||||||
|
buff[0] = (byte) (val >> 8);
|
||||||
|
buff[1] = (byte) val;
|
||||||
|
out.write(buff, 0, 2);
|
||||||
|
written += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int writeShortToBuffer(int val,
|
||||||
|
byte[] buffer, int offset) throws TIOException {
|
||||||
|
buffer[offset++] = (byte) (val >> 8);
|
||||||
|
buffer[offset++] = (byte) val;
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the specified encoded in {@link DataInput modified UTF-8} to this
|
||||||
|
* stream.
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* the string to write to the target stream encoded in
|
||||||
|
* {@link DataInput modified UTF-8}.
|
||||||
|
* @throws IOException
|
||||||
|
* if an error occurs while writing to the target stream.
|
||||||
|
* @throws UTFDataFormatException
|
||||||
|
* if the encoded string is longer than 65535 bytes.
|
||||||
|
* @see DataInputStream#readUTF()
|
||||||
|
*/
|
||||||
|
public final void writeUTF(TString str) throws TIOException {
|
||||||
|
long utfCount = countUTFBytes(str);
|
||||||
|
if (utfCount > 65535) {
|
||||||
|
throw new TIOException(TString.wrap("UTF Error"));
|
||||||
|
}
|
||||||
|
byte[] buffer = new byte[(int)utfCount + 2];
|
||||||
|
int offset = 0;
|
||||||
|
offset = writeShortToBuffer((int) utfCount, buffer, offset);
|
||||||
|
offset = writeUTFBytesToBuffer(str, (int) utfCount, buffer, offset);
|
||||||
|
write(buffer, 0, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
long countUTFBytes(TString str) {
|
||||||
|
int utfCount = 0, length = str.length();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int charValue = str.charAt(i);
|
||||||
|
if (charValue > 0 && charValue <= 127) {
|
||||||
|
utfCount++;
|
||||||
|
} else if (charValue <= 2047) {
|
||||||
|
utfCount += 2;
|
||||||
|
} else {
|
||||||
|
utfCount += 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return utfCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
int writeUTFBytesToBuffer(TString str, long count,
|
||||||
|
byte[] buffer, int offset) throws TIOException {
|
||||||
|
int length = str.length();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int charValue = str.charAt(i);
|
||||||
|
if (charValue > 0 && charValue <= 127) {
|
||||||
|
buffer[offset++] = (byte) charValue;
|
||||||
|
} else if (charValue <= 2047) {
|
||||||
|
buffer[offset++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
|
||||||
|
buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
|
||||||
|
} else {
|
||||||
|
buffer[offset++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
|
||||||
|
buffer[offset++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
|
||||||
|
buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation. Codename One designates this
|
||||||
|
* particular file as subject to the "Classpath" exception as provided
|
||||||
|
* by Oracle in the LICENSE file that accompanied this code.
|
||||||
|
*
|
||||||
|
* This code 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
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Codename One through http://www.codenameone.com/ if you
|
||||||
|
* need additional information or have any questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.teavm.classlib.java.io;
|
||||||
|
import org.teavm.classlib.java.lang.*;
|
||||||
|
/**
|
||||||
|
* An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are translated into bytes. The encoding that it uses may be specified by name, or the platform's default encoding may be accepted.
|
||||||
|
* Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.
|
||||||
|
* Since: CLDC 1.0 See Also:Writer, UnsupportedEncodingException
|
||||||
|
*/
|
||||||
|
public class TOutputStreamWriter extends TWriter{
|
||||||
|
private TOutputStream os;
|
||||||
|
private TString enc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an OutputStreamWriter that uses the default character encoding.
|
||||||
|
* os - An OutputStream
|
||||||
|
*/
|
||||||
|
public TOutputStreamWriter(TOutputStream os){
|
||||||
|
this.os = os;
|
||||||
|
enc = TString.wrap("UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an OutputStreamWriter that uses the named character encoding.
|
||||||
|
* os - An OutputStreamenc - The name of a supported
|
||||||
|
* - If the named encoding is not supported
|
||||||
|
*/
|
||||||
|
public TOutputStreamWriter(TOutputStream os, TString enc) throws TUnsupportedEncodingException{
|
||||||
|
this.os = os;
|
||||||
|
this.enc = enc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the stream.
|
||||||
|
*/
|
||||||
|
public void close() throws TIOException{
|
||||||
|
os.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flush the stream.
|
||||||
|
*/
|
||||||
|
public void flush() throws TIOException{
|
||||||
|
os.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a portion of an array of characters.
|
||||||
|
*/
|
||||||
|
public void write(char[] cbuf, int off, int len) throws TIOException{
|
||||||
|
write(new TString(cbuf, off, len));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a single character.
|
||||||
|
*/
|
||||||
|
public void write(int c) throws TIOException{
|
||||||
|
write(new TString(new char[] {(char)c}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a portion of a string.
|
||||||
|
*/
|
||||||
|
public void write(TString str, int off, int len) throws TIOException{
|
||||||
|
if(off > 0 || len != str.length()) {
|
||||||
|
str = str.substring(off, len);
|
||||||
|
}
|
||||||
|
os.write(str.getBytes(enc));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,108 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation. Codename One designates this
|
||||||
|
* particular file as subject to the "Classpath" exception as provided
|
||||||
|
* by Oracle in the LICENSE file that accompanied this code.
|
||||||
|
*
|
||||||
|
* This code 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
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Codename One through http://www.codenameone.com/ if you
|
||||||
|
* need additional information or have any questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.teavm.classlib.java.io;
|
||||||
|
|
||||||
|
import org.teavm.classlib.java.lang.*;
|
||||||
|
/**
|
||||||
|
* Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
|
||||||
|
* Since: JDK1.1, CLDC 1.0 See Also:OutputStreamWriter, Reader
|
||||||
|
*/
|
||||||
|
public abstract class TWriter extends TObject{
|
||||||
|
/**
|
||||||
|
* The object used to synchronize operations on this stream. For efficiency, a character-stream object may use an object other than itself to protect critical sections. A subclass should therefore use the object in this field rather than this or a synchronized method.
|
||||||
|
*/
|
||||||
|
protected TObject lock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new character-stream writer whose critical sections will synchronize on the writer itself.
|
||||||
|
*/
|
||||||
|
protected TWriter(){
|
||||||
|
lock = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new character-stream writer whose critical sections will synchronize on the given object.
|
||||||
|
* lock - Object to synchronize on.
|
||||||
|
*/
|
||||||
|
protected TWriter(TObject lock){
|
||||||
|
this.lock = lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.
|
||||||
|
*/
|
||||||
|
public abstract void close() throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
|
||||||
|
*/
|
||||||
|
public abstract void flush() throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write an array of characters.
|
||||||
|
*/
|
||||||
|
public void write(char[] cbuf) throws TIOException{
|
||||||
|
write(cbuf, 0, cbuf.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a portion of an array of characters.
|
||||||
|
*/
|
||||||
|
public abstract void write(char[] cbuf, int off, int len) throws TIOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
|
||||||
|
* Subclasses that intend to support efficient single-character output should override this method.
|
||||||
|
*/
|
||||||
|
public void write(int c) throws TIOException{
|
||||||
|
synchronized (lock) {
|
||||||
|
char oneCharArray[] = new char[1];
|
||||||
|
oneCharArray[0] = (char) c;
|
||||||
|
write(oneCharArray);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a string.
|
||||||
|
*/
|
||||||
|
public void write(TString str) throws TIOException{
|
||||||
|
write(str, 0, str.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a portion of a string.
|
||||||
|
*/
|
||||||
|
public void write(TString str, int off, int len) throws TIOException{
|
||||||
|
if (len < 0) {
|
||||||
|
throw new StringIndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
char buf[] = new char[len];
|
||||||
|
str.getChars(off, off + len, buf, 0);
|
||||||
|
|
||||||
|
synchronized (lock) {
|
||||||
|
write(buf, 0, buf.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user