package org.codehaus.mojo.unix.util.line;
/*
* The MIT License
*
* Copyright 2009 The Codehaus.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.codehaus.plexus.util.*;
import java.io.*;
/**
* A lazy writer that won't throw any exceptions until close() is called. If there is an exception while writing,
* it will be stored and throw later on. If an exception is thrown while streaming <em>and</em> when the stream is
* closed, the exception that was caught during streaming will be thrown from <code>close()</code>. The stream will
* still be closed.
*
* There is no flushing during streaming.
*/
public class LineWriterWriter
extends AbstractLineStreamWriter
implements Flushable, Closeable
{
private final Writer writer;
private IOException e;
public LineWriterWriter( Writer writer )
{
this.writer = writer;
}
protected void onLine( String line )
{
if ( e != null )
{
return;
}
try
{
if ( line != null )
{
writer.write( line );
}
writer.write( EOL );
}
catch ( IOException e )
{
this.e = e;
}
}
public void close()
throws IOException
{
try
{
if ( e != null )
{
throw e;
}
}
finally
{
IOUtil.close( writer );
}
}
public void flush()
throws IOException
{
try
{
writer.flush();
}
catch ( IOException e )
{
this.e = e;
}
}
}