mirror of
https://github.com/corda/corda.git
synced 2025-06-17 06:38:21 +00:00
Added java.util.Formatter implementation. Basic/common formats work,
such as %s, %d, %x... also width, left justify, zerofill flags are implemented. Many of the other formats do not work, see the comments in avian.FormatString javadoc and look for FIXME and TODO items for more information on features that are known to not work correctly.
This commit is contained in:
@ -78,6 +78,25 @@ public class PrintStream extends OutputStream {
|
||||
print(String.valueOf(s));
|
||||
}
|
||||
|
||||
public synchronized void printf(java.util.Locale locale, String format, Object... args) {
|
||||
// should this be cached in an instance variable??
|
||||
final java.util.Formatter formatter = new java.util.Formatter(this);
|
||||
formatter.format(locale, format, args);
|
||||
}
|
||||
|
||||
public synchronized void printf(String format, Object... args) {
|
||||
final java.util.Formatter formatter = new java.util.Formatter(this);
|
||||
formatter.format(format, args);
|
||||
}
|
||||
|
||||
public void format(String format, Object... args) {
|
||||
printf(format, args);
|
||||
}
|
||||
|
||||
public void format(java.util.Locale locale, String format, Object... args) {
|
||||
printf(locale, format, args);
|
||||
}
|
||||
|
||||
public synchronized void println(String s) {
|
||||
try {
|
||||
out.write(s.getBytes());
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
package java.io;
|
||||
|
||||
public abstract class Writer implements Closeable, Flushable {
|
||||
public abstract class Writer implements Closeable, Flushable, Appendable {
|
||||
public void write(int c) throws IOException {
|
||||
char[] buffer = new char[] { (char) c };
|
||||
write(buffer);
|
||||
@ -33,6 +33,30 @@ public abstract class Writer implements Closeable, Flushable {
|
||||
public abstract void write(char[] buffer, int offset, int length)
|
||||
throws IOException;
|
||||
|
||||
public Appendable append(final char c) throws IOException {
|
||||
write((int)c);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Appendable append(final CharSequence sequence) throws IOException {
|
||||
return append(sequence, 0, sequence.length());
|
||||
}
|
||||
|
||||
public Appendable append(CharSequence sequence, int start, int end)
|
||||
throws IOException {
|
||||
final int length = end - start;
|
||||
if (sequence instanceof String) {
|
||||
write((String)sequence, start, length);
|
||||
} else {
|
||||
final char[] charArray = new char[length];
|
||||
for (int i = start; i < end; i++) {
|
||||
charArray[i] = sequence.charAt(i);
|
||||
}
|
||||
write(charArray, 0, length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract void flush() throws IOException;
|
||||
|
||||
public abstract void close() throws IOException;
|
||||
|
@ -13,6 +13,7 @@ package java.lang;
|
||||
import java.io.Serializable;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Formatter;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -574,6 +575,20 @@ public final class String
|
||||
|
||||
public native String intern();
|
||||
|
||||
public static String format(String fmt, Object... args) {
|
||||
final Formatter formatter = new Formatter();
|
||||
final String result = formatter.format(fmt, args).toString();
|
||||
formatter.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String format(Locale l, String fmt, Object... args) {
|
||||
final Formatter formatter = new Formatter();
|
||||
final String result = formatter.format(l, fmt, args).toString();
|
||||
formatter.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String valueOf(Object s) {
|
||||
return s == null ? "null" : s.toString();
|
||||
}
|
||||
|
158
classpath/java/util/Formatter.java
Normal file
158
classpath/java/util/Formatter.java
Normal file
@ -0,0 +1,158 @@
|
||||
/* 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.util;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.Flushable;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Writer;
|
||||
|
||||
import avian.FormatString;
|
||||
|
||||
public class Formatter implements Closeable, Flushable, AutoCloseable {
|
||||
|
||||
private final Appendable _out;
|
||||
|
||||
private final Locale _locale;
|
||||
|
||||
private IOException lastException;
|
||||
|
||||
private boolean _closed;
|
||||
|
||||
private void ensureNotClosed() {
|
||||
if (this._closed) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
public Formatter() {
|
||||
this((Appendable) null, (Locale) null);
|
||||
}
|
||||
|
||||
public Formatter(final Appendable a) {
|
||||
this(a, null);
|
||||
}
|
||||
|
||||
public Formatter(final Appendable a, final Locale l) {
|
||||
if (a == null) {
|
||||
this._out = new StringBuilder();
|
||||
} else {
|
||||
this._out = a;
|
||||
}
|
||||
if (l == null) {
|
||||
this._locale = Locale.getDefault();
|
||||
} else {
|
||||
this._locale = l;
|
||||
}
|
||||
}
|
||||
|
||||
public Formatter(File file) throws IOException {
|
||||
this(file, null);
|
||||
}
|
||||
|
||||
public Formatter(File file, Locale l) throws IOException {
|
||||
this(new FileWriter(file), l);
|
||||
}
|
||||
|
||||
public Formatter(Locale l) {
|
||||
this((Appendable) null, l);
|
||||
}
|
||||
|
||||
public Formatter(OutputStream os) {
|
||||
this(os, null);
|
||||
}
|
||||
|
||||
public Formatter(OutputStream os, Locale l) {
|
||||
this(new OutputStreamWriter(os), l);
|
||||
}
|
||||
|
||||
public Formatter(PrintStream ps) {
|
||||
this(new OutputStreamWriter(ps));
|
||||
}
|
||||
|
||||
public Formatter(String fileName) throws IOException {
|
||||
this(fileName, (Locale) null);
|
||||
}
|
||||
|
||||
public Formatter(String fileName, Locale l) throws IOException {
|
||||
this(new File(fileName), l);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (this._closed) {
|
||||
return;
|
||||
}
|
||||
final Appendable out = out();
|
||||
if (out instanceof Closeable) {
|
||||
final Closeable closeable = (Closeable) out;
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("An error occurred while closing.", e);
|
||||
}
|
||||
this._closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
ensureNotClosed();
|
||||
final Appendable out = out();
|
||||
if (out instanceof Flushable) {
|
||||
final Flushable flushable = (Flushable) out;
|
||||
try {
|
||||
flushable.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("An error occurred while flushing.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Formatter format(Locale l, final String format, final Object...args) {
|
||||
ensureNotClosed();
|
||||
try {
|
||||
final FormatString formatString = FormatString.compile(format);
|
||||
this._out.append(formatString.format(args));
|
||||
} catch (IOException e) {
|
||||
this.lastException = e;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Formatter format(String format, Object... args) {
|
||||
ensureNotClosed();
|
||||
return this.format(null, format, args);
|
||||
}
|
||||
|
||||
public IOException ioException() {
|
||||
return lastException;
|
||||
}
|
||||
|
||||
public Locale locale() {
|
||||
ensureNotClosed();
|
||||
return this._locale;
|
||||
}
|
||||
|
||||
public Appendable out() {
|
||||
ensureNotClosed();
|
||||
return this._out;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
ensureNotClosed();
|
||||
return this._out.toString();
|
||||
}
|
||||
|
||||
}
|
31
classpath/java/util/IllegalFormatException.java
Normal file
31
classpath/java/util/IllegalFormatException.java
Normal file
@ -0,0 +1,31 @@
|
||||
/* 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.util;
|
||||
|
||||
public class IllegalFormatException extends IllegalArgumentException {
|
||||
|
||||
public IllegalFormatException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public IllegalFormatException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public IllegalFormatException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public IllegalFormatException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user