netty5/src/main/java/org/jboss/netty/buffer/ChannelBufferOutputStream.java
2008-08-10 05:52:36 +00:00

140 lines
4.0 KiB
Java

/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.buffer;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* {@link OutputStream} which writes data to a {@link ChannelBuffer}.
* <p>
* A write operation against this stream will occur at the {@code writerIndex}
* of its underlying buffer and the {@code writerIndex} will increase during
* the write operation.
* <p>
* This stream implements {@link DataOutput} for your convenience.
* The endianness of the stream is not always big endian but depends on
* the endianness of the underlying buffer.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*
* @see ChannelBufferInputStream
* @apiviz.uses org.jboss.netty.buffer.ChannelBuffer
*/
public class ChannelBufferOutputStream extends OutputStream implements DataOutput {
private final ChannelBuffer buffer;
private final DataOutputStream utf8out = new DataOutputStream(this);
/**
* Creates a new stream which writes data to the specified {@code buffer}.
*/
public ChannelBufferOutputStream(ChannelBuffer buffer) {
if (buffer == null) {
throw new NullPointerException("buffer");
}
this.buffer = buffer;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (len == 0) {
return;
}
buffer.writeBytes(b, off, len);
}
@Override
public void write(byte[] b) throws IOException {
buffer.writeBytes(b);
}
@Override
public void write(int b) throws IOException {
buffer.writeByte((byte) b);
}
public void writeBoolean(boolean v) throws IOException {
write(v? (byte) 1 : (byte) 0);
}
public void writeByte(int v) throws IOException {
write(v);
}
public void writeBytes(String s) throws IOException {
int len = s.length();
for (int i = 0; i < len; i ++) {
write((byte) s.charAt(i));
}
}
public void writeChar(int v) throws IOException {
writeShort((short) v);
}
public void writeChars(String s) throws IOException {
int len = s.length();
for (int i = 0 ; i < len ; i ++) {
writeChar(s.charAt(i));
}
}
public void writeDouble(double v) throws IOException {
writeLong(Double.doubleToLongBits(v));
}
public void writeFloat(float v) throws IOException {
writeInt(Float.floatToIntBits(v));
}
public void writeInt(int v) throws IOException {
buffer.writeInt(v);
}
public void writeLong(long v) throws IOException {
buffer.writeLong(v);
}
public void writeShort(int v) throws IOException {
buffer.writeShort((short) v);
}
public void writeUTF(String s) throws IOException {
utf8out.writeUTF(s);
}
/**
* Returns the buffer where this stream is writing data.
*/
public ChannelBuffer buffer() {
return buffer;
}
}