Fix for last commit.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.daemon;
21 import java.nio.ByteBuffer;
22 import java.nio.CharBuffer;
23 import java.nio.channels.ClosedChannelException;
24 import java.nio.charset.Charset;
25 import java.nio.charset.CharsetEncoder;
26 import java.nio.charset.CoderResult;
29 * Encodes a line to buffers using the correct charset.
30 * @author Christian Lins
36 private CharBuffer characters;
37 private Charset charset;
40 * Constructs new LineEncoder.
44 public LineEncoder(CharBuffer characters, Charset charset)
46 this.characters = characters;
47 this.charset = charset;
51 * Encodes the characters of this instance to the given ChannelLineBuffers
52 * using the Charset of this instance.
54 * @throws java.nio.channels.ClosedChannelException
56 public void encode(ChannelLineBuffers buffer)
57 throws ClosedChannelException
59 CharsetEncoder encoder = charset.newEncoder();
60 while (characters.hasRemaining())
62 ByteBuffer buf = ChannelLineBuffers.newLineBuffer();
63 assert buf.position() == 0;
64 assert buf.capacity() >= 512;
66 CoderResult res = encoder.encode(characters, buf, true);
68 // Set limit to current position and current position to 0;
69 // means make ready for read from buffer
71 buffer.addOutputBuffer(buf);
73 if (res.isUnderflow()) // All input processed