diff options
Diffstat (limited to 'simple/simple-http/src/test/java/org/simpleframework/http/core/MockSender.java')
-rw-r--r-- | simple/simple-http/src/test/java/org/simpleframework/http/core/MockSender.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/simple/simple-http/src/test/java/org/simpleframework/http/core/MockSender.java b/simple/simple-http/src/test/java/org/simpleframework/http/core/MockSender.java new file mode 100644 index 0000000..eb20930 --- /dev/null +++ b/simple/simple-http/src/test/java/org/simpleframework/http/core/MockSender.java @@ -0,0 +1,75 @@ +package org.simpleframework.http.core; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import org.simpleframework.common.buffer.ArrayBuffer; +import org.simpleframework.common.buffer.Buffer; +import org.simpleframework.transport.ByteCursor; +import org.simpleframework.transport.ByteWriter; + +public class MockSender implements ByteWriter { + + private Buffer buffer; + + public MockSender() { + this(1024); + } + + public MockSender(int size) { + this.buffer = new ArrayBuffer(size); + } + + public Buffer getBuffer() { + return buffer; + } + + public ByteCursor getCursor() throws IOException { + return new StreamCursor(buffer.encode("UTF-8")); + } + + public void write(byte[] array) throws IOException { + buffer.append(array); + } + + public void write(byte[] array, int off, int len) throws IOException { + buffer.append(array, off, len); + } + + public void flush() throws IOException { + return; + } + + public void close() throws IOException { + return; + } + + public String toString() { + return buffer.toString(); + } + + public boolean isOpen() throws Exception { + return true; + } + + public void write(ByteBuffer source) throws IOException { + int mark = source.position(); + int limit = source.limit(); + + byte[] array = new byte[limit - mark]; + source.get(array, 0, array.length); + buffer.append(array); + } + + public void write(ByteBuffer source, int off, int len) throws IOException { + int mark = source.position(); + int limit = source.limit(); + + if(limit - mark < len) { + len = limit - mark; + } + byte[] array = new byte[len]; + source.get(array, 0, len); + buffer.append(array); + } +} |