001 /*
002 * Copyright (C) 2008 Trustin Heuiseung Lee
003 *
004 * This library is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License as published by the Free Software Foundation; either
007 * version 2.1 of the License, or (at your option) any later version.
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this library; if not, write to the Free Software
016 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA
017 */
018 package net.gleamynode.netty.buffer;
019
020 import java.io.DataOutput;
021 import java.io.DataOutputStream;
022 import java.io.IOException;
023 import java.io.OutputStream;
024
025 /**
026 *
027 * @author The Netty Project (netty@googlegroups.com)
028 * @author Trustin Lee (trustin@gmail.com)
029 *
030 * @version $Rev$, $Date$
031 *
032 * @see ChannelBufferInputStream
033 * @apiviz.uses net.gleamynode.netty.buffer.ChannelBuffer
034 */
035 public class ChannelBufferOutputStream extends OutputStream implements DataOutput {
036
037 private final ChannelBuffer buffer;
038 private final DataOutputStream utf8out = new DataOutputStream(this);
039
040 public ChannelBufferOutputStream(ChannelBuffer buffer) {
041 if (buffer == null) {
042 throw new NullPointerException("buffer");
043 }
044 this.buffer = buffer;
045 }
046
047 @Override
048 public void write(byte[] b, int off, int len) throws IOException {
049 if (len == 0) {
050 return;
051 }
052
053 buffer.writeBytes(b, off, len);
054 }
055
056 @Override
057 public void write(byte[] b) throws IOException {
058 buffer.writeBytes(b);
059 }
060
061 @Override
062 public void write(int b) throws IOException {
063 buffer.writeByte((byte) b);
064 }
065
066 public void writeBoolean(boolean v) throws IOException {
067 write(v? (byte) 1 : (byte) 0);
068 }
069
070 public void writeByte(int v) throws IOException {
071 write(v);
072 }
073
074 public void writeBytes(String s) throws IOException {
075 int len = s.length();
076 for (int i = 0; i < len; i ++) {
077 write((byte) s.charAt(i));
078 }
079 }
080
081 public void writeChar(int v) throws IOException {
082 writeShort((short) v);
083 }
084
085 public void writeChars(String s) throws IOException {
086 int len = s.length();
087 for (int i = 0 ; i < len ; i ++) {
088 writeChar(s.charAt(i));
089 }
090 }
091
092 public void writeDouble(double v) throws IOException {
093 writeLong(Double.doubleToLongBits(v));
094 }
095
096 public void writeFloat(float v) throws IOException {
097 writeInt(Float.floatToIntBits(v));
098 }
099
100 public void writeInt(int v) throws IOException {
101 buffer.writeInt(v);
102 }
103
104 public void writeLong(long v) throws IOException {
105 buffer.writeLong(v);
106 }
107
108 public void writeShort(int v) throws IOException {
109 buffer.writeShort((short) v);
110 }
111
112 public void writeUTF(String s) throws IOException {
113 utf8out.writeUTF(s);
114 }
115
116 public ChannelBuffer buffer() {
117 return buffer;
118 }
119 }