下面列出了怎么用java.io.Flushable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public String readLine(String prompt, Character mask)
throws IOException
{
String line;
interrupted = false;
try {
line = super.readLine(prompt, mask);
}
catch (UserInterruptException e) {
interrupted = true;
return null;
}
if (getHistory() instanceof Flushable) {
((Flushable) getHistory()).flush();
}
return line;
}
private void connect(final Readable source, final Appendable sink) {
Thread thread = new Thread(new Runnable() {
public void run() {
CharBuffer cb = CharBuffer.wrap(new char [256]);
try {
while (source.read(cb) != -1) {
cb.flip();
sink.append(cb);
cb.clear();
}
if (sink instanceof Flushable) {
((Flushable)sink).flush();
}
} catch (IOException e) { /* prolly broken pipe, just die */ }
}
});
thread.setDaemon(true);
thread.start();
}
public StateWrapper(WrappedState<E, ER, Q, QR> wrappedState, SerializeExtensionPoint serializeExtensionPoint) {
this.serializeExtensionPoint = serializeExtensionPoint;
this.wrappedState = wrappedState;
if (wrappedState instanceof Flushable) {
flushable = (Flushable) wrappedState;
} else {
flushable = null;
}
}
@Override
protected void done() throws IOException {
if (closeable instanceof Flushable)
((Flushable) closeable).flush();
if (ignoreCloseExceptions)
try {
closeable.close();
} catch (IOException e) {
// Ignored
}
else
closeable.close();
}
@Override
protected void done() throws IOException {
if (closeable instanceof Flushable)
((Flushable) closeable).flush();
if (ignoreCloseExceptions)
try {
closeable.close();
} catch (IOException e) {
// Ignored
}
else
closeable.close();
}
public void close() {
this.baseImplementation.stop();
AppenderAttachableImpl var1 = this.downstreamAppenders;
synchronized(this.downstreamAppenders) {
Enumeration enumer = this.downstreamAppenders.getAllAppenders();
while(enumer != null && enumer.hasMoreElements()) {
Appender appender = (Appender)enumer.nextElement();
if (appender instanceof Flushable) {
try {
((Flushable)appender).flush();
} catch (Exception var6) {
;
}
}
}
enumer = this.downstreamAppenders.getAllAppenders();
while(true) {
if (enumer == null || !enumer.hasMoreElements()) {
break;
}
((Appender)enumer.nextElement()).close();
}
}
this.closed = true;
}
public AnsiConsole(Appendable target, Flushable flushable, ColorMap colorMap) {
this.target = target;
this.flushable = flushable;
this.colorMap = colorMap;
container = new Screen();
textArea = new TextAreaImpl(container);
}
public AnsiConsole(Appendable target, Flushable flushable, ColorMap colorMap) {
this.target = target;
this.flushable = flushable;
this.colorMap = colorMap;
container = new Screen();
textArea = new TextAreaImpl(container);
}
private void tryFlush() {
if (!(out instanceof Flushable))
return;
try {
((Flushable) out).flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void flushQuietly(Flushable flushable) {
if (flushable == null) return;
try {
flushable.flush();
} catch (Exception e) {
OkLogger.printStackTrace(e);
}
}
public static void flushQuietly(Flushable flushable) {
if (flushable == null) return;
try {
flushable.flush();
} catch (Exception e) {
OkLogger.printStackTrace(e);
}
}
public static void flushQuietly(Flushable flushable) {
if (flushable != null)
try {
flushable.flush();
} catch (Exception ignored) {
}
}
public static void flushOrLog(Flushable f, String message) {
if (f != null) {
try {
f.flush();
} catch (IOException e) {
Logger.getLogger().e(message, e);
}
}
}
/**
* Flush the flushable and catch any exception thrown.
*
* @param flushable the flushable to flush
* @return any Exception thrown during the <code>flush</code> operation
*/
public static Exception flush(Flushable flushable) {
if (flushable != null) {
try {
flushable.flush();
}
catch (Exception e) {
return e;
}
}
return null;
}
@Override
protected void done() throws IOException {
if (closeable instanceof Flushable)
((Flushable) closeable).flush();
if (ignoreCloseExceptions)
try {
closeable.close();
} catch (IOException e) {
// Ignored
}
else
closeable.close();
}
public Writer flush() {
try {
if (appendable instanceof Flushable) {
Flushable flushable = (Flushable) appendable;
flushable.flush();
}
} catch (java.io.IOException e) { throw new IOException(e); }
return this;
}
protected void done() throws IOException {
if (this.closeable instanceof Flushable) {
((Flushable) this.closeable).flush();
}
if (this.ignoreCloseExceptions) {
try {
this.closeable.close();
return;
} catch (IOException e) {
return;
}
}
this.closeable.close();
}
public static void flushOrLog(Flushable f, String message) {
if (f != null) {
try {
f.flush();
} catch (IOException e) {
Fabric.getLogger().e(Fabric.TAG, message, e);
}
}
}
/**
* Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.
*/
public static void flushQuietly(Flushable flushable) {
if (flushable != null) {
try {
flushable.flush();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
/**
* Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.
*/
public static void flushQuietly(Flushable flushable) {
if (flushable != null) {
try {
flushable.flush();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
/**
* Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.
*/
public static void flushQuietly(Flushable flushable) {
if (flushable != null) {
try {
flushable.flush();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
@SuppressWarnings("unchecked")
private static void flushAppenders(Logger l) {
final Enumeration<Appender> allAppenders = l.getAllAppenders();
while (allAppenders.hasMoreElements()) {
final Appender a = allAppenders.nextElement();
if (a instanceof Flushable) {
try {
((Flushable) a).flush();
} catch (IOException ioe) {
System.err.println(a + ": Failed to flush!"
+ StringUtils.stringifyException(ioe));
}
}
}
}
@Override
protected void done() throws IOException {
if (closeable instanceof Flushable)
((Flushable) closeable).flush();
if (ignoreCloseExceptions)
try {
closeable.close();
} catch (IOException e) {
// Ignored
}
else
closeable.close();
}
private static void close( Closeable[] streams, int idx ) throws IOException
{
if( idx >= streams.length )
{
return; // done
}
Closeable stream = streams[idx];
try
{
if( stream != null )
{
if( stream instanceof Flushable )
{
((Flushable)stream).flush();
}
stream.close();
}
}
catch( IOException ex )
{
if( !(stream instanceof InputStream || stream instanceof Reader) )
{
throw ex; // ignore io exceptions for input streams and readers
}
}
finally
{
close( streams, idx + 1 );
}
}
public static void flushQuietly(Flushable flushable) {
if (flushable != null) {
try {
flushable.flush();
} catch (Exception ignored) {
}
}
}
@SuppressWarnings("unchecked")
private static void flushAppenders(Logger l) {
final Enumeration<Appender> allAppenders = l.getAllAppenders();
while (allAppenders.hasMoreElements()) {
final Appender a = allAppenders.nextElement();
if (a instanceof Flushable) {
try {
((Flushable) a).flush();
} catch (IOException ioe) {
System.err.println(a + ": Failed to flush!"
+ StringUtils.stringifyException(ioe));
}
}
}
}
public static void flushQuietly(Flushable flushable) {
if (flushable == null) return;
try {
flushable.flush();
} catch (Exception e) {
OkLogger.printStackTrace(e);
}
}
public static void flush(Flushable flushable) {
try {
flushable.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Flush a {@link Flushable}, with control over whether an {@code IOException} may be thrown.
*
* <p>If {@code swallowIOException} is true, then we don't rethrow {@code IOException}, but merely
* log it.
*
* @param flushable the {@code Flushable} object to be flushed.
* @param swallowIOException if true, don't propagate IO exceptions thrown by the {@code flush}
* method
* @throws IOException if {@code swallowIOException} is false and {@link Flushable#flush} throws
* an {@code IOException}.
* @see Closeables#close
*/
public static void flush(Flushable flushable, boolean swallowIOException) throws IOException {
try {
flushable.flush();
} catch (IOException e) {
if (swallowIOException) {
logger.log(Level.WARNING, "IOException thrown while flushing Flushable.", e);
} else {
throw e;
}
}
}
/**
* Equivalent to calling {@code flush(flushable, true)}, but with no {@code IOException} in the
* signature.
*
* @param flushable the {@code Flushable} object to be flushed.
*/
public static void flushQuietly(Flushable flushable) {
try {
flush(flushable, true);
} catch (IOException e) {
logger.log(Level.SEVERE, "IOException should not have been thrown.", e);
}
}