diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/Charset.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/Charset.java index d2cc3c882..ad47c0de0 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/Charset.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/Charset.java @@ -25,7 +25,7 @@ public abstract class Charset { public abstract void decode(ByteBuffer source, CharBuffer dest); public static Charset get(String name) { - if (name.equals("UTF-8")) { + if (name.toUpperCase().equals("UTF-8")) { return new UTF8Charset(); } return null; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java index 8d701a40f..a69ee9e86 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java @@ -39,7 +39,7 @@ public class TBufferedInputStream extends TFilterInputStream { } @Override - public synchronized int available() throws TIOException { + public int available() throws TIOException { TInputStream localIn = in; if (buf == null || localIn == null) { throw new TIOException(TString.wrap("Stream is closed")); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TByteArrayOutputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TByteArrayOutputStream.java index 07c17c87e..69c991994 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TByteArrayOutputStream.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TByteArrayOutputStream.java @@ -51,7 +51,7 @@ public class TByteArrayOutputStream extends TOutputStream { private void ensureCapacity(int capacity) { if (buf.length < capacity) { - capacity = TMath.min(capacity, buf.length * 3 / 2); + capacity = TMath.max(capacity, buf.length * 3 / 2); buf = TArrays.copyOf(buf, capacity); } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TDataOutputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TDataOutputStream.java index 7064857ac..2e771de0e 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TDataOutputStream.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TDataOutputStream.java @@ -17,6 +17,7 @@ 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 @@ -33,7 +34,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu * {@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. */ @@ -45,7 +46,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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. */ @@ -56,7 +57,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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() { @@ -69,7 +70,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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 @@ -95,7 +96,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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 @@ -110,13 +111,14 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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() */ + @Override public final void writeBoolean(boolean val) throws TIOException { out.write(val ? 1 : 0); written++; @@ -125,7 +127,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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 @@ -133,6 +135,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu * @see DataInputStream#readByte() * @see DataInputStream#readUnsignedByte() */ + @Override public final void writeByte(int val) throws TIOException { out.write(val); written++; @@ -140,7 +143,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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 @@ -164,13 +167,14 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu * 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() */ + @Override public final void writeChar(int val) throws TIOException { buff[0] = (byte) (val >> 8); buff[1] = (byte) val; @@ -181,7 +185,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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. @@ -189,6 +193,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu * if an error occurs while writing to the target stream. * @see DataInputStream#readChar() */ + @Override public final void writeChars(TString str) throws TIOException { byte newBytes[] = new byte[str.length() * 2]; for (int index = 0; index < str.length(); index++) { @@ -203,13 +208,14 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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() */ + @Override public final void writeDouble(double val) throws TIOException { writeLong(TDouble.doubleToLongBits(val)); } @@ -217,13 +223,14 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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() */ + @Override public final void writeFloat(float val) throws TIOException { writeInt(TFloat.floatToIntBits(val)); } @@ -231,13 +238,14 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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() */ + @Override public final void writeInt(int val) throws TIOException { buff[0] = (byte) (val >> 24); buff[1] = (byte) (val >> 16); @@ -250,13 +258,14 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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() */ + @Override public final void writeLong(long val) throws TIOException { buff[0] = (byte) (val >> 56); buff[1] = (byte) (val >> 48); @@ -287,7 +296,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu * 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 @@ -295,6 +304,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu * @see DataInputStream#readShort() * @see DataInputStream#readUnsignedShort() */ + @Override public final void writeShort(int val) throws TIOException { buff[0] = (byte) (val >> 8); buff[1] = (byte) val; @@ -312,7 +322,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu /** * 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}. @@ -322,15 +332,16 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu * if the encoded string is longer than 65535 bytes. * @see DataInputStream#readUTF() */ + @Override public final void writeUTF(TString str) throws TIOException { long utfCount = countUTFBytes(str); if (utfCount > 65535) { - throw new TIOException(TString.wrap("UTF Error")); + 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); + offset = writeUTFBytesToBuffer(str, buffer, offset); write(buffer, 0, offset); } @@ -349,8 +360,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu return utfCount; } - int writeUTFBytesToBuffer(TString str, long count, - byte[] buffer, int offset) throws TIOException { + int writeUTFBytesToBuffer(TString str, byte[] buffer, int offset) throws TIOException { int length = str.length(); for (int i = 0; i < length; i++) { int charValue = str.charAt(i); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterInputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterInputStream.java index a391efde5..fe7d27796 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterInputStream.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterInputStream.java @@ -34,7 +34,7 @@ public class TFilterInputStream extends TInputStream { } @Override - public synchronized void mark(int readlimit) { + public void mark(int readlimit) { in.mark(readlimit); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TOutputStreamWriter.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TOutputStreamWriter.java index 6a7de47e3..9063ff528 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TOutputStreamWriter.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TOutputStreamWriter.java @@ -1,92 +1,125 @@ /* - * 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. + * Copyright 2015 Alexey Andreev. + * + * Licensed 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.*; -/** - * 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"); + +import org.teavm.classlib.impl.charset.ByteBuffer; +import org.teavm.classlib.impl.charset.CharBuffer; +import org.teavm.classlib.impl.charset.Charset; +import org.teavm.classlib.java.lang.TString; + +public class TOutputStreamWriter extends TWriter { + private TOutputStream out; + private String encoding; + private Charset charset; + private byte[] bufferData = new byte[512]; + private ByteBuffer buffer = new ByteBuffer(bufferData); + + public TOutputStreamWriter(TOutputStream out) { + this(out, "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); + public TOutputStreamWriter(TOutputStream out, final String enc) throws TUnsupportedEncodingException { + super(out); + if (enc == null) { + throw new NullPointerException(); } - os.write(str.getBytes(enc)); + this.out = out; + charset = Charset.get(enc); + if (charset == null) { + throw new TUnsupportedEncodingException(TString.wrap(enc)); + } + encoding = enc; } + @Override + public void close() throws TIOException { + if (charset != null) { + flush(); + charset = null; + out.flush(); + out.close(); + } + } + + @Override + public void flush() throws TIOException { + checkStatus(); + if (buffer.position() > 0) { + out.write(bufferData, 0, buffer.position()); + buffer.rewind(0); + } + out.flush(); + } + + private void checkStatus() throws TIOException { + if (charset == null) { + throw new TIOException(TString.wrap("Writer already closed")); + } + } + + public String getEncoding() { + return encoding; + } + + @Override + public void write(char[] buf, int offset, int count) throws TIOException { + synchronized (lock) { + checkStatus(); + if (buf == null) { + throw new NullPointerException(); + } + if (offset < 0 || offset > buf.length - count || count < 0) { + throw new IndexOutOfBoundsException(); + } + CharBuffer input = new CharBuffer(buf, offset, offset + count); + while (!input.end()) { + if (buffer.available() < 6) { + out.write(bufferData, 0, buffer.position()); + buffer.rewind(0); + } + charset.encode(input, buffer); + } + } + } + + @Override + public void write(int oneChar) throws TIOException { + synchronized (lock) { + checkStatus(); + CharBuffer input = new CharBuffer(new char[] { (char)oneChar }, 0, 1); + while (!input.end()) { + if (buffer.available() < 6) { + out.write(bufferData, 0, buffer.position()); + buffer.rewind(0); + } + charset.encode(input, buffer); + } + } + } + + @Override + public void write(String str, int offset, int count) throws TIOException { + if (str == null) { + throw new NullPointerException(); + } + if (count < 0) { + throw new IndexOutOfBoundsException("Negative count: " + count); + } + char[] chars = new char[count]; + str.getChars(offset, offset + count, chars, 0); + write(chars); + } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TWriter.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TWriter.java index b20a35726..4155d3b57 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TWriter.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TWriter.java @@ -1,108 +1,82 @@ /* - * 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. + * Copyright 2015 Alexey Andreev. + * + * Licensed 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.*; -/** - * 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; +import org.teavm.classlib.java.lang.TAppendable; +import org.teavm.classlib.java.lang.TCharSequence; - /** - * Create a new character-stream writer whose critical sections will synchronize on the writer itself. - */ - protected TWriter(){ +public abstract class TWriter implements TAppendable, TCloseable, TFlushable { + protected Object lock; + + protected TWriter() { + super(); 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){ + protected TWriter(Object lock) { + if (lock == null) { + throw new NullPointerException(); + } 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); + public void write(char buf[]) throws TIOException { + write(buf, 0, buf.length); } - /** - * Write a portion of an array of characters. - */ - public abstract void write(char[] cbuf, int off, int len) throws TIOException; + public abstract void write(char buf[], int offset, int count) 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{ + public void write(int oneChar) throws TIOException { synchronized (lock) { char oneCharArray[] = new char[1]; - oneCharArray[0] = (char) c; + oneCharArray[0] = (char) oneChar; write(oneCharArray); } } - /** - * Write a string. - */ - public void write(TString str) throws TIOException{ + public void write(String 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) { + public void write(String str, int offset, int count) throws TIOException { + if (count < 0) { throw new StringIndexOutOfBoundsException(); } - char buf[] = new char[len]; - str.getChars(off, off + len, buf, 0); - + char buf[] = new char[count]; + str.getChars(offset, offset + count, buf, 0); synchronized (lock) { write(buf, 0, buf.length); } } + @Override + public TWriter append(char c) throws TIOException { + write(c); + return this; + } + + @Override + public TWriter append(TCharSequence csq) throws TIOException { + write(csq != null ? csq.toString() : "null"); + return this; + } + + @Override + public TWriter append(TCharSequence csq, int start, int end) throws TIOException { + write(csq != null ? csq.subSequence(start, end).toString() : "null"); + return this; + } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index cf2cd483e..57cb9e0cc 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -96,16 +96,20 @@ public class TObject { } o.monitor.owner = null; - Platform.startThread(new PlatformRunnable() { - @Override public void run() { - if (o.isEmptyMonitor() || o.monitor.owner != null) { - return; + if (!o.monitor.enteringThreads.isEmpty()) { + Platform.startThread(new PlatformRunnable() { + @Override public void run() { + if (o.isEmptyMonitor() || o.monitor.owner != null) { + return; + } + if (!o.monitor.enteringThreads.isEmpty()) { + o.monitor.enteringThreads.remove().run(); + } } - if (!o.monitor.enteringThreads.isEmpty()) { - o.monitor.enteringThreads.remove().run(); - } - } - }); + }); + } else { + o.isEmptyMonitor(); + } } boolean isEmptyMonitor() { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java index b0350ac7f..0d9b5ac21 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java @@ -101,7 +101,7 @@ public class TThrowable extends RuntimeException { } @Override - public synchronized Throwable fillInStackTrace() { + public Throwable fillInStackTrace() { return this; } @@ -116,7 +116,7 @@ public class TThrowable extends RuntimeException { } @Override - public synchronized TThrowable getCause() { + public TThrowable getCause() { return cause != this ? cause : null; } @@ -126,7 +126,7 @@ public class TThrowable extends RuntimeException { @Remove public native TString toString0(); - public synchronized TThrowable initCause(TThrowable cause) { + public TThrowable initCause(TThrowable cause) { if (this.cause != this && this.cause != null) { throw new TIllegalStateException(TString.wrap("Cause already set")); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java index dc4561ad9..0ebde76ff 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCalendar.java @@ -293,7 +293,7 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl return value; } - public static synchronized TLocale[] getAvailableLocales() { + public static TLocale[] getAvailableLocales() { return TLocale.getAvailableLocales(); } @@ -303,11 +303,11 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl abstract public int getGreatestMinimum(int field); - public static synchronized TCalendar getInstance() { + public static TCalendar getInstance() { return new TGregorianCalendar(); } - public static synchronized TCalendar getInstance(TLocale locale) { + public static TCalendar getInstance(TLocale locale) { return new TGregorianCalendar(locale); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java index 34a9b130f..2c0aa68e3 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TLocale.java @@ -244,11 +244,11 @@ public final class TLocale implements TCloneable, TSerializable { } @Override - public synchronized int hashCode() { + public int hashCode() { return countryCode.hashCode() + languageCode.hashCode() + variantCode.hashCode(); } - public synchronized static void setDefault(TLocale locale) { + public static void setDefault(TLocale locale) { if (locale != null) { defaultLocale = locale; } else { diff --git a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java index 0bf1e44dd..0c47fd733 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -1399,6 +1399,12 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.ws().append("<<").ws().append("16)").ws().append(">>").ws().append("16"); exitPriority(); break; + case INT_TO_CHAR: + enterPriority(Priority.BITWISE_AND, Associativity.LEFT, true); + expr.getOperand().acceptVisitor(this); + writer.ws().append("&").ws().append("65535"); + exitPriority(); + break; case NULL_CHECK: enterPriority(Priority.COMMA, Associativity.NONE, false); writer.append("$rt_nullCheck("); diff --git a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java index 653405d5c..2400494ab 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java @@ -307,9 +307,11 @@ class StatementGenerator implements InstructionVisitor { value = Expr.unary(UnaryOperation.INT_TO_BYTE, value); break; case SHORT: - case CHARACTER: value = Expr.unary(UnaryOperation.INT_TO_SHORT, value); break; + case CHARACTER: + value = Expr.unary(UnaryOperation.INT_TO_CHAR, value); + break; } break; case TO_INTEGER: diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java b/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java index ae7d69434..03c9ad303 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java @@ -31,5 +31,6 @@ public enum UnaryOperation { INT_TO_LONG, INT_TO_BYTE, INT_TO_SHORT, + INT_TO_CHAR, NULL_CHECK } diff --git a/teavm-core/src/main/resources/org/teavm/tooling/test/res/junit-support.js b/teavm-core/src/main/resources/org/teavm/tooling/test/res/junit-support.js index 18f25d016..bde8a9555 100644 --- a/teavm-core/src/main/resources/org/teavm/tooling/test/res/junit-support.js +++ b/teavm-core/src/main/resources/org/teavm/tooling/test/res/junit-support.js @@ -391,14 +391,19 @@ JUnitClient.run = function() { } JUnitClient.makeErrorMessage = function(message, e) { message.status = "exception"; + var stack = ""; + while (e instanceof TeaVMAsyncError) { + stack += e.message + "\n" + e.stack + "\n"; + e = e.cause; + } if (e.$javaException && e.$javaException.constructor.$meta) { message.exception = e.$javaException.constructor.$meta.name; message.stack = e.$javaException.constructor.$meta.name + ": "; var exceptionMessage = extractException(e.$javaException); message.stack += exceptionMessage ? $rt_ustr(exceptionMessage) : ""; - message.stack += "\n" + e.stack; + message.stack += e.stack + "\n" + stack; } else { - message.stack = e.stack; + message.stack = stack; } } JUnitClient.reportError = function(error) { diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java index 65c160c71..9e6fc50cc 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java @@ -186,7 +186,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin private void generateSchedule(GeneratorContext context, SourceWriter writer, boolean timeout) throws IOException { String runnable = context.getParameterName(1); writer.append("return window.setTimeout(function()").ws().append("{").indent().softNewLine(); - String methodName = writer.getNaming().getFullNameForAsync(new MethodReference(Platform.class, "launchThread", + String methodName = writer.getNaming().getFullNameFor(new MethodReference(Platform.class, "launchThread", PlatformRunnable.class, void.class)); writer.append("$rt_rootInvocationAdapter(").append(methodName).append(")(").append(runnable).append(");") .softNewLine(); diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/io/OutputStreamWriterTest.java b/teavm-tests/src/test/java/org/teavm/classlib/java/io/OutputStreamWriterTest.java new file mode 100644 index 000000000..54ac67f58 --- /dev/null +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/io/OutputStreamWriterTest.java @@ -0,0 +1,506 @@ +/* + * 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 java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import org.junit.Test; +import static org.junit.Assert.*; + +public class OutputStreamWriterTest { + + private static final int UPPER = 0xd800; + + private static final int BUFFER_SIZE = 10000; + + private ByteArrayOutputStream out; + + private OutputStreamWriter writer; + + static private final String source = "This is a test message with Unicode character. " + + "\u4e2d\u56fd is China's name in Chinese"; + + static private final String[] MINIMAL_CHARSETS = { "UTF-8" }; + + OutputStreamWriter osw; + + InputStreamReader isr; + + private ByteArrayOutputStream fos; + + String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream" + + "\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n"; + + public OutputStreamWriterTest() throws UnsupportedEncodingException { + out = new ByteArrayOutputStream(); + writer = new OutputStreamWriter(out, "utf-8"); + + fos = new ByteArrayOutputStream(); + osw = new OutputStreamWriter(fos); + } + + @Test + public void testClose() throws Exception { + writer.flush(); + writer.close(); + try { + writer.flush(); + fail(); + } catch (IOException e) { + // Expected + } + } + + @Test + public void testFlush() throws Exception { + writer.write(source); + writer.flush(); + String result = out.toString("utf-8"); + assertEquals(source, result); + } + + @Test + public void testWritecharArrayintint() throws IOException { + char[] chars = source.toCharArray(); + + // Throws IndexOutOfBoundsException if offset is negative + try { + writer.write((char[])null, -1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (NullPointerException | IndexOutOfBoundsException e) { + // Expected + } + + // throws NullPointerException though count is negative + try { + writer.write((char[])null, 1, -1); + fail("should throw NullPointerException"); + } catch (NullPointerException | IndexOutOfBoundsException e) { + // Expected + } + + try { + writer.write((char[])null, 1, 1); + fail(); + } catch (NullPointerException e) { + // Expected + } + try { + writer.write(new char[0], 0, 1); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + try { + writer.write(chars, -1, 1); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + try { + writer.write(chars, 0, -1); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + try { + writer.write(chars, 1, chars.length); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + writer.write(chars, 1, 2); + writer.flush(); + assertEquals("hi", out.toString("utf-8")); + writer.write(chars, 0, chars.length); + writer.flush(); + assertEquals("hi" + source, out.toString("utf-8")); + + writer.close(); + // After the stream is closed, should throw IOException first + try { + writer.write((char[])null, -1, -1); + fail("should throw IOException"); + } catch (IOException e) { + // Expected + } + } + + @Test + public void testWriteint() throws IOException { + writer.write(1); + writer.flush(); + String str = new String(out.toByteArray(), "utf-8"); + assertEquals("\u0001", str); + + writer.write(2); + writer.flush(); + str = new String(out.toByteArray(), "utf-8"); + assertEquals("\u0001\u0002", str); + + writer.write(-1); + writer.flush(); + str = new String(out.toByteArray(), "utf-8"); + assertEquals("\u0001\u0002\uffff", str); + + writer.write(0xfedcb); + writer.flush(); + str = new String(out.toByteArray(), "utf-8"); + assertEquals("\u0001\u0002\uffff\uedcb", str); + + writer.close(); + // After the stream is closed, should throw IOException + try { + writer.write(1); + fail("should throw IOException"); + } catch (IOException e) { + // expected + } + } + + @Test + public void testWriteStringintint() throws IOException { + try { + writer.write((String)null, 1, 1); + fail(); + } catch (NullPointerException e) { + // Expected + } + try { + writer.write("", 0, 1); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + try { + writer.write("abc", -1, 1); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + try { + writer.write("abc", 0, -1); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + try { + writer.write("abc", 1, 3); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + + // Throws IndexOutOfBoundsException before NullPointerException if count + // is negative + try { + writer.write((String)null, -1, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException | NullPointerException e) { + // Expected + } + + // Throws NullPointerException before StringIndexOutOfBoundsException + try { + writer.write((String)null, -1, 0); + fail("should throw NullPointerException"); + } catch (IndexOutOfBoundsException | NullPointerException e) { + // expected + } + + writer.write("abc", 1, 2); + writer.flush(); + assertEquals("bc", out.toString("utf-8")); + writer.write(source, 0, source.length()); + writer.flush(); + assertEquals("bc" + source, out.toString("utf-8")); + + writer.close(); + // Throws IndexOutOfBoundsException first if count is negative + try { + writer.write((String)null, 0, -1); + fail("should throw IndexOutOfBoundsException"); + } catch (NullPointerException | IndexOutOfBoundsException e) { + // Expected + } + + try { + writer.write((String)null, -1, 0); + fail("should throw NullPointerException"); + } catch (NullPointerException | IndexOutOfBoundsException e) { + // Expected + } + + try { + writer.write("abc", -1, 0); + fail("should throw StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // Expected + } + + // Throws IOException + try { + writer.write("abc", 0, 1); + fail("should throw IOException"); + } catch (IOException e) { + // expected + } + } + + @Test + public void testOutputStreamWriterOutputStream() throws IOException { + try { + writer = new OutputStreamWriter(null); + fail(); + } catch (NullPointerException e) { + // Expected + } + OutputStreamWriter writer2 = new OutputStreamWriter(out); + writer2.close(); + } + + @Test + public void testOutputStreamWriterOutputStreamString() throws IOException { + try { + writer = new OutputStreamWriter(null, "utf-8"); + fail(); + } catch (NullPointerException e) { + // Expected + } + try { + writer = new OutputStreamWriter(out, ""); + fail(); + } catch (UnsupportedEncodingException e) { + // Expected + } + try { + writer = new OutputStreamWriter(out, "badname"); + fail(); + } catch (UnsupportedEncodingException e) { + // Expected + } + try { + writer = new OutputStreamWriter(out, (String)null); + fail(); + } catch (NullPointerException e) { + // Expected + } + } + + @Test + public void testSingleCharIO() throws Exception { + InputStreamReader isr = null; + for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) { + try { + out = new ByteArrayOutputStream(); + writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]); + + int upper = UPPER; + switch (i) { + case 0: + upper = 128; + break; + case 1: + upper = 256; + break; + } + + for (int c = 0; c < upper; ++c) { + writer.write(c); + } + writer.flush(); + byte[] result = out.toByteArray(); + + isr = new InputStreamReader(new ByteArrayInputStream(result), MINIMAL_CHARSETS[i]); + for (int expected = 0; expected < upper; ++expected) { + assertEquals("Error when reading bytes in " + MINIMAL_CHARSETS[i], expected, isr.read()); + } + } finally { + try { + isr.close(); + } catch (Exception e) { + } + try { + writer.close(); + } catch (Exception e) { + } + } + } + } + + @Test + public void testBlockIO() throws Exception { + InputStreamReader isr = null; + char[] largeBuffer = new char[BUFFER_SIZE]; + for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) { + try { + out = new ByteArrayOutputStream(); + writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]); + + int upper = UPPER; + switch (i) { + case 0: + upper = 128; + break; + case 1: + upper = 256; + break; + } + + int m = 0; + for (int c = 0; c < upper; ++c) { + largeBuffer[m++] = (char)c; + if (m == BUFFER_SIZE) { + writer.write(largeBuffer); + m = 0; + } + } + writer.write(largeBuffer, 0, m); + writer.flush(); + byte[] result = out.toByteArray(); + + isr = new InputStreamReader(new ByteArrayInputStream(result), MINIMAL_CHARSETS[i]); + int expected = 0, read = 0, j = 0; + while (expected < upper) { + if (j == read) { + read = isr.read(largeBuffer); + j = 0; + } + assertEquals("Error when reading bytes in " + MINIMAL_CHARSETS[i], expected++, largeBuffer[j++]); + } + } finally { + try { + isr.close(); + } catch (Exception e) { + } + try { + writer.close(); + } catch (Exception e) { + } + } + } + } + + @Test + public void test_ConstructorLjava_io_OutputStream() { + assertTrue("Used in tests", true); + } + + @Test + public void test_ConstructorLjava_io_OutputStreamLjava_lang_String() throws UnsupportedEncodingException { + osw = new OutputStreamWriter(fos, "UTF-8"); + try { + osw = new OutputStreamWriter(fos, "Bogus"); + fail("Failed to throw Unsupported Encoding exception"); + } catch (UnsupportedEncodingException e) { + // Expected + } + } + + @Test + public void test_close() throws IOException { + osw.close(); + + try { + osw.write(testString, 0, testString.length()); + fail("Chars written after close"); + } catch (IOException e) { + // Expected + } + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + try { + OutputStreamWriter writer = new OutputStreamWriter(bout, "ISO2022JP"); + writer.write(new char[] { 'a' }); + writer.close(); + // the default is ASCII, there should not be any mode changes + String converted = new String(bout.toByteArray(), "ISO8859_1"); + assertTrue("invalid conversion 1: " + converted, converted.equals("a")); + + bout.reset(); + writer = new OutputStreamWriter(bout, "ISO2022JP"); + writer.write(new char[] { '\u3048' }); + writer.flush(); + // the byte sequence should not switch to ASCII mode until the + // stream is closed + converted = new String(bout.toByteArray(), "ISO8859_1"); + assertTrue("invalid conversion 2: " + converted, converted.equals("\u001b$B$(")); + writer.close(); + converted = new String(bout.toByteArray(), "ISO8859_1"); + assertTrue("invalid conversion 3: " + converted, converted.equals("\u001b$B$(\u001b(B")); + + bout.reset(); + writer = new OutputStreamWriter(bout, "ISO2022JP"); + writer.write(new char[] { '\u3048' }); + writer.write(new char[] { '\u3048' }); + writer.close(); + // there should not be a mode switch between writes + assertEquals("invalid conversion 4", "\u001b$B$($(\u001b(B", new String(bout.toByteArray(), "ISO8859_1")); + } catch (UnsupportedEncodingException e) { + // Can't test missing converter + System.out.println(e); + } + } + + @Test + public void test_flush() throws IOException { + char[] buf = new char[testString.length()]; + osw.write(testString, 0, testString.length()); + osw.flush(); + openInputStream(); + isr.read(buf, 0, buf.length); + assertTrue("Chars not flushed", new String(buf, 0, buf.length).equals(testString)); + } + + @Test + public void test_write$CII() throws IOException { + char[] buf = new char[testString.length()]; + osw.write(testString, 0, testString.length()); + osw.close(); + openInputStream(); + isr.read(buf, 0, buf.length); + assertTrue("Incorrect chars returned", new String(buf, 0, buf.length).equals(testString)); + } + + @Test + public void test_writeI() throws IOException { + osw.write('T'); + osw.close(); + openInputStream(); + int c = isr.read(); + assertEquals("Incorrect char returned", 'T', (char)c); + } + + @Test + public void test_writeLjava_lang_StringII() throws IOException { + char[] buf = new char[testString.length()]; + osw.write(testString, 0, testString.length()); + osw.close(); + openInputStream(); + isr.read(buf); + assertEquals("Incorrect chars returned", testString, new String(buf, 0, buf.length)); + } + + private void openInputStream() { + isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray())); + } +}