Replace OutputStreamWriter and DataOutputStream

This commit is contained in:
Alexey Andreev 2015-02-26 19:12:53 +04:00
parent 0b7db410d3
commit a47cf14352
17 changed files with 746 additions and 205 deletions

View File

@ -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;

View File

@ -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"));

View File

@ -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);
}
}

View File

@ -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
@ -117,6 +118,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
* 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++;
@ -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++;
@ -171,6 +174,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 writeChar(int val) throws TIOException {
buff[0] = (byte) (val >> 8);
buff[1] = (byte) val;
@ -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++) {
@ -210,6 +215,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
* 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));
}
@ -224,6 +230,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
* 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));
}
@ -238,6 +245,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
* 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);
@ -257,6 +265,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
* 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);
@ -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;
@ -322,6 +332,7 @@ 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) {
@ -330,7 +341,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
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);

View File

@ -34,7 +34,7 @@ public class TFilterInputStream extends TInputStream {
}
@Override
public synchronized void mark(int readlimit) {
public void mark(int readlimit) {
in.mark(readlimit);
}

View File

@ -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.
* Copyright 2015 Alexey Andreev.
*
* 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).
* 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
*
* 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.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
* 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
*/
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 os;
private TString enc;
private TOutputStream out;
private String encoding;
private Charset charset;
private byte[] bufferData = new byte[512];
private ByteBuffer buffer = new ByteBuffer(bufferData);
/**
* Create an OutputStreamWriter that uses the default character encoding.
* os - An OutputStream
*/
public TOutputStreamWriter(TOutputStream os){
this.os = os;
enc = TString.wrap("UTF-8");
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;
public TOutputStreamWriter(TOutputStream out, final String enc) throws TUnsupportedEncodingException {
super(out);
if (enc == null) {
throw new NullPointerException();
}
this.out = out;
charset = Charset.get(enc);
if (charset == null) {
throw new TUnsupportedEncodingException(TString.wrap(enc));
}
encoding = enc;
}
/**
* Close the stream.
*/
@Override
public void close() throws TIOException {
os.close();
if (charset != null) {
flush();
charset = null;
out.flush();
out.close();
}
}
/**
* Flush the stream.
*/
@Override
public void flush() throws TIOException {
os.flush();
checkStatus();
if (buffer.position() > 0) {
out.write(bufferData, 0, buffer.position());
buffer.rewind(0);
}
out.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));
private void checkStatus() throws TIOException {
if (charset == null) {
throw new TIOException(TString.wrap("Writer already closed"));
}
}
/**
* Write a single character.
*/
public void write(int c) throws TIOException{
write(new TString(new char[] {(char)c}));
public String getEncoding() {
return encoding;
}
/**
* 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);
@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);
}
}
os.write(str.getBytes(enc));
}
@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);
}
}

View File

@ -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.
* Copyright 2015 Alexey Andreev.
*
* 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).
* 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
*
* 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.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
* 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;
public abstract class TWriter implements TAppendable, TCloseable, TFlushable {
protected Object lock;
/**
* Create a new character-stream writer whose critical sections will synchronize on the writer itself.
*/
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;
}
}

View File

@ -96,6 +96,7 @@ public class TObject {
}
o.monitor.owner = null;
if (!o.monitor.enteringThreads.isEmpty()) {
Platform.startThread(new PlatformRunnable() {
@Override public void run() {
if (o.isEmptyMonitor() || o.monitor.owner != null) {
@ -106,6 +107,9 @@ public class TObject {
}
}
});
} else {
o.isEmptyMonitor();
}
}
boolean isEmptyMonitor() {

View File

@ -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"));
}

View File

@ -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);
}

View File

@ -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 {

View File

@ -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(");

View File

@ -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:

View File

@ -31,5 +31,6 @@ public enum UnaryOperation {
INT_TO_LONG,
INT_TO_BYTE,
INT_TO_SHORT,
INT_TO_CHAR,
NULL_CHECK
}

View File

@ -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) {

View File

@ -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();

View File

@ -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()));
}
}