summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/core/StreamCursor.java
blob: d6f6a0990d12397e5d910bdca79a2cd9f5fd28da (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
package org.simpleframework.http.core;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.simpleframework.http.StreamTransport;
import org.simpleframework.transport.ByteCursor;
import org.simpleframework.transport.Transport;
import org.simpleframework.transport.TransportCursor;

public class StreamCursor implements ByteCursor {

   private TransportCursor cursor;
   private Transport transport;
   private byte[] swap;
   
   public StreamCursor(String source) throws IOException {
      this(source.getBytes("UTF-8"));
   }
   
   public StreamCursor(byte[] data) throws IOException {
      this(new ByteArrayInputStream(data));
   }
   
   public StreamCursor(InputStream source) throws IOException {
      this.transport = new StreamTransport(source, new OutputStream() {
         public void write(int octet){}
      });
      this.cursor = new TransportCursor(transport);
      this.swap = new byte[1];
   }

   // TODO investigate this
   public boolean isOpen() throws IOException {
      return true;
   }
   
   public boolean isReady() throws IOException {
      return cursor.isReady();
   }
   
   public int ready() throws IOException {
      return cursor.ready();
   }
   
   public int read() throws IOException {
      if(read(swap) > 0) {
         return swap[0] & 0xff;
      }
      return 0;
   }

   public int read(byte[] data) throws IOException {
      return read(data, 0, data.length);
   }

   public int read(byte[] data, int off, int len) throws IOException {
      return cursor.read(data, off, len);
   }

   public int reset(int len) throws IOException {      
      return cursor.reset(len);
   }

   public void push(byte[] data) throws IOException {
      push(data, 0, data.length);
   }

   public void push(byte[] data, int off, int len) throws IOException {
      cursor.push(data, off, len);
   }
}