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    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.IOException;
021    import java.io.OutputStream;
022    import java.io.Writer;
023    
024    import java.nio.ByteBuffer;
025    import java.nio.CharBuffer;
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, StringPool.DEFAULT_CHARSET_NAME);
035            }
036    
037            public OutputStreamWriter(OutputStream outputStream, String charsetName) {
038                    if (charsetName == null) {
039                            charsetName = StringPool.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 CharsetEncoder _charsetEncoder;
088            private String _charsetName;
089            private OutputStream _outputStream;
090    
091    }