summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/core/MockSender.java
blob: eb209301bbffaf5217655f464156944c7a8a31d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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);
   }
}