From 0537cb4775839be812a6ee36fab36766f8ea0083 Mon Sep 17 00:00:00 2001 From: Marcin Olawski Date: Sun, 8 Feb 2015 16:09:08 +0100 Subject: [PATCH 1/2] Add ByteArrayOutputStream.writeTo(OutputStream), UTFDataFormatException and Modifier.isTransient(int). --- classpath/java/io/ByteArrayOutputStream.java | 22 +++++++++++++++++++ classpath/java/io/UTFDataFormatException.java | 21 ++++++++++++++++++ classpath/java/lang/reflect/Modifier.java | 1 + 3 files changed, 44 insertions(+) create mode 100644 classpath/java/io/UTFDataFormatException.java diff --git a/classpath/java/io/ByteArrayOutputStream.java b/classpath/java/io/ByteArrayOutputStream.java index 264e32833a..7aceb4b718 100644 --- a/classpath/java/io/ByteArrayOutputStream.java +++ b/classpath/java/io/ByteArrayOutputStream.java @@ -98,6 +98,28 @@ public class ByteArrayOutputStream extends OutputStream { return array; } + public synchronized void writeTo(OutputStream out) throws IOException { + if (length==0) + return; + + flushBuffer(); + + int cnt = 0; + for (Cell c = chain; c != null; c = c.next) { + cnt++; + } + + Cell[] cellArray = new Cell[cnt]; + + for (Cell c = chain; c != null; c = c.next) { + cellArray[--cnt] = c; + } + + for (Cell c : cellArray){ + out.write(c.array, c.offset, c.length); + } + } + @Override public String toString() { return new String(toByteArray()); diff --git a/classpath/java/io/UTFDataFormatException.java b/classpath/java/io/UTFDataFormatException.java new file mode 100644 index 0000000000..7938bbf6b0 --- /dev/null +++ b/classpath/java/io/UTFDataFormatException.java @@ -0,0 +1,21 @@ +/* Copyright (c) 2008-2015, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.io; + +public class UTFDataFormatException extends IOException { + public UTFDataFormatException(String s) { + super(s); + } + + public UTFDataFormatException() { + super(); + } +} diff --git a/classpath/java/lang/reflect/Modifier.java b/classpath/java/lang/reflect/Modifier.java index 5d2ff22eef..c5451b1983 100644 --- a/classpath/java/lang/reflect/Modifier.java +++ b/classpath/java/lang/reflect/Modifier.java @@ -32,6 +32,7 @@ public final class Modifier { public static boolean isProtected(int v) { return (v & PROTECTED) != 0; } public static boolean isStatic (int v) { return (v & STATIC) != 0; } public static boolean isFinal (int v) { return (v & FINAL) != 0; } + public static boolean isTransient(int v) { return (v & TRANSIENT) != 0; } public static boolean isSuper (int v) { return (v & SUPER) != 0; } public static boolean isNative (int v) { return (v & NATIVE) != 0; } public static boolean isAbstract (int v) { return (v & ABSTRACT) != 0; } From a5ff62fbadd916b685e939a518a1c7bd0e9e29e6 Mon Sep 17 00:00:00 2001 From: Marcin Olawski Date: Sun, 8 Feb 2015 22:38:56 +0100 Subject: [PATCH 2/2] Swap the directionality of the links in Cell. --- classpath/java/io/ByteArrayOutputStream.java | 65 ++++++++++---------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/classpath/java/io/ByteArrayOutputStream.java b/classpath/java/io/ByteArrayOutputStream.java index 7aceb4b718..b7cf7f7bb1 100644 --- a/classpath/java/io/ByteArrayOutputStream.java +++ b/classpath/java/io/ByteArrayOutputStream.java @@ -13,7 +13,8 @@ package java.io; public class ByteArrayOutputStream extends OutputStream { private static final int BufferSize = 32; - private Cell chain; + private Cell firstCell; + private Cell curCell; private int length; private byte[] buffer; private int position; @@ -25,7 +26,8 @@ public class ByteArrayOutputStream extends OutputStream { } public void reset() { - chain = null; + firstCell = null; + curCell = null; length = 0; buffer = null; position = 0; @@ -69,11 +71,21 @@ public class ByteArrayOutputStream extends OutputStream { position += length; } else { flushBuffer(); - chain = new Cell(copy(b, offset, length), 0, length, chain); + chainCell( new Cell(copy(b, offset, length), 0, length) ); } this.length += length; } + + private void chainCell(Cell cell){ + if (curCell == null){ + firstCell = cell; + curCell = cell; + }else{ + curCell.next = cell; + curCell = cell; + } + } private void flushBuffer() { if (position > 0) { @@ -81,7 +93,8 @@ public class ByteArrayOutputStream extends OutputStream { int p = position; buffer = null; position = 0; - chain = new Cell(b, 0, p, chain); + + chainCell( new Cell(b, 0, p) ); } } @@ -89,35 +102,26 @@ public class ByteArrayOutputStream extends OutputStream { flushBuffer(); byte[] array = new byte[length]; - int index = length; - for (Cell c = chain; c != null; c = c.next) { - int start = index - c.length; - System.arraycopy(c.array, c.offset, array, start, c.length); - index = start; + int pos = 0; + for (Cell c = firstCell; c != null; c = c.next) { + System.arraycopy(c.array, c.offset, array, pos, c.length); + pos += c.length; } return array; } public synchronized void writeTo(OutputStream out) throws IOException { - if (length==0) - return; - - flushBuffer(); - - int cnt = 0; - for (Cell c = chain; c != null; c = c.next) { - cnt++; - } - - Cell[] cellArray = new Cell[cnt]; - - for (Cell c = chain; c != null; c = c.next) { - cellArray[--cnt] = c; - } - - for (Cell c : cellArray){ - out.write(c.array, c.offset, c.length); - } + if (length==0) return; + + if (out == null){ + throw new NullPointerException(); + } + + flushBuffer(); + + for (Cell c = firstCell; c != null; c = c.next) { + out.write(c.array, c.offset, c.length); + } } @Override @@ -133,13 +137,12 @@ public class ByteArrayOutputStream extends OutputStream { public byte[] array; public int offset; public int length; - public Cell next; + public Cell next = null; - public Cell(byte[] array, int offset, int length, Cell next) { + public Cell(byte[] array, int offset, int length) { this.array = array; this.offset = offset; this.length = length; - this.next = next; } } }