001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io;
016    
017    import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    import java.io.Writer;
022    
023    import java.nio.ByteBuffer;
024    import java.nio.CharBuffer;
025    import java.nio.charset.Charset;
026    import java.nio.charset.CharsetEncoder;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class OutputStreamWriter extends Writer {
032    
033            public OutputStreamWriter(OutputStream outputStream) {
034                    this(outputStream, _DEFAULT_CHARSET_NAME);
035            }
036    
037            public OutputStreamWriter(OutputStream outputStream, String charsetName) {
038                    if (charsetName == null) {
039                            charsetName = _DEFAULT_CHARSET_NAME;
040                    }
041    
042                    _outputStream = outputStream;
043                    _charsetName = charsetName;
044                    _charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(charsetName);
045            }
046    
047            @Override
048            public void close() throws IOException {
049                    _outputStream.close();
050            }
051    
052            @Override
053            public void flush() throws IOException {
054                    _outputStream.flush();
055            }
056    
057            public String getEncoding() {
058                    return _charsetName;
059            }
060    
061            @Override
062            public void write(char[] chars, int offset, int length) throws IOException {
063                    ByteBuffer byteBuffer = _charsetEncoder.encode(
064                            CharBuffer.wrap(chars, offset, length));
065    
066                    _outputStream.write(byteBuffer.array(), 0, byteBuffer.limit());
067            }
068    
069            @Override
070            public void write(int c) throws IOException {
071                    ByteBuffer byteBuffer = _charsetEncoder.encode(
072                            CharBuffer.wrap(new char[] {(char)c}));
073    
074                    _outputStream.write(byteBuffer.array(), 0, byteBuffer.limit());
075            }
076    
077            @Override
078            public void write(String string, int offset, int length)
079                    throws IOException {
080    
081                    ByteBuffer byteBuffer = _charsetEncoder.encode(
082                            CharBuffer.wrap(string, offset, offset + length));
083    
084                    _outputStream.write(byteBuffer.array(), 0, byteBuffer.limit());
085            }
086    
087            private static final String _DEFAULT_CHARSET_NAME =
088                    Charset.defaultCharset().name();
089    
090            private CharsetEncoder _charsetEncoder;
091            private String _charsetName;
092            private OutputStream _outputStream;
093    
094    }