001    /**
002     * Copyright (c) 2000-2012 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.UTF8);
035            }
036    
037            public OutputStreamWriter(OutputStream outputStream, String charsetName) {
038                    _outputStream = outputStream;
039                    _charsetName = charsetName;
040                    _charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(charsetName);
041            }
042    
043            @Override
044            public void close() throws IOException {
045                    _outputStream.close();
046            }
047    
048            @Override
049            public void flush() throws IOException {
050                    _outputStream.flush();
051            }
052    
053            public String getEncoding() {
054                    return _charsetName;
055            }
056    
057            @Override
058            public void write(char[] chars, int offset, int length) throws IOException {
059                    ByteBuffer byteBuffer = _charsetEncoder.encode(
060                            CharBuffer.wrap(chars, offset, length));
061    
062                    _outputStream.write(byteBuffer.array(), 0, byteBuffer.limit());
063            }
064    
065            @Override
066            public void write(int c) throws IOException {
067                    ByteBuffer byteBuffer = _charsetEncoder.encode(
068                            CharBuffer.wrap(new char[] {(char)c}));
069    
070                    _outputStream.write(byteBuffer.array(), 0, byteBuffer.limit());
071            }
072    
073            @Override
074            public void write(String string, int offset, int length)
075                    throws IOException {
076    
077                    ByteBuffer byteBuffer = _charsetEncoder.encode(
078                            CharBuffer.wrap(string, offset, length));
079    
080                    _outputStream.write(byteBuffer.array(), 0, byteBuffer.limit());
081            }
082    
083            private CharsetEncoder _charsetEncoder;
084            private String _charsetName;
085            private OutputStream _outputStream;
086    
087    }