Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
**** BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.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.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2009 Yoko Harada <yokolet@gmail.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. **** END LICENSE BLOCK *****
 
 package org.jruby.embed.io;
 
 import java.io.Writer;
A WriterOutputStream converts java.io.Writer to java.io.OutputStream.

Author(s):
Yoko Harada <yokolet@gmail.com>
 
 public class WriterOutputStream extends OutputStream {
 
     private final Writer writer;
     private boolean isOpen = true;
     private CharsetDecoder decoder;

    
Creates WriterOutputStream from given java.io.Writer object with a default encoding.

Parameters:
writer java.io.Writer object to be converted to.
 
     public WriterOutputStream(Writer writer) {
         this(writernull);
     }

    
Creates WriterOutputStream from given java.io.Writer object with a specified encoding.

Parameters:
writer java.io.Writer object to be converted to.
 
     public WriterOutputStream(Writer writerString encoding) {
         this. = writer;       
         if (encoding == null && writer instanceof OutputStreamWriter) {
             // this encoding might be null when writer has been closed
             encoding = ((OutputStreamWriterwriter).getEncoding();
         }
         if (encoding == null) {
             encoding = Charset.defaultCharset().name();
         } else if (!Charset.isSupported(encoding)) {
             throw new IllegalArgumentException(encoding + " is not supported");
         }
          = Charset.forName(encoding).newDecoder();
     }

    
Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Throws:
java.io.IOException if an I/O error occurs.
 
     @Override
     public void close() throws IOException {
         synchronized () {
             if (!) {
                 throw new IOException("This stream has been already closed.");
             }
              = false;
             = null;
            .close();
        }
    }

    
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Throws:
java.io.IOException if an I/O error occurs.
    @Override
    public void flush() throws IOException {
        synchronized () {
            if (!) {
                return;
            }
            .flush();
        }
    }

    
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters:
b the byte.
Throws:
java.io.IOException if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    @Override
    public void write(int bthrows IOException {
        byte[] bb = new byte[]{(byteb};
        write(bb, 0, 1);
    }

    
Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters:
b the data.
Throws:
java.io.IOException if an I/O error occurs.
See also:
java.io.OutputStream.write(byte[],int,int)
    @Override
    public void write(byte[] bthrows IOException {
        write(b, 0, b.length);
    }

    
Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters:
b the data.
off the start offset in the data.
len the number of bytes to write.
Throws:
java.io.IOException if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
    @Override
    public void write(byte[] bint offint lenthrows IOException {
        synchronized () {
            if (!) {
                return;
            }
            if (off < 0 || len <= 0 || (off + len) > b.length) {
                throw new IndexOutOfBoundsException();
            }
            ByteBuffer bytes = ByteBuffer.wrap(bofflen);
            CharBuffer chars = CharBuffer.allocate(len);
            byte2char(byteschars);
            char[] cbuf = new char[chars.length()];
            chars.get(cbuf, 0, chars.length());
            .write(cbuf);
            .flush();
        }
    }
    private void byte2char(ByteBuffer bytesCharBuffer charsthrows IOException {
        .reset();
        chars.clear();
        CoderResult result = .decode(bytescharstrue);
        if (result.isError() || result.isOverflow()) {
            throw new IOException(result.toString());
        } else if (result.isUnderflow()) {
            chars.flip();
        }
    }
New to GrepCode? Check out our FAQ X