1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.util;
21
22 import java.io.InputStream;
23 import java.nio.ByteBuffer;
24
25
26
27
28
29
30
31
32
33
34
35 public class ByteBufferInputStream extends InputStream {
36
37 private final ByteBuffer buffer;
38
39
40
41
42
43
44 public ByteBufferInputStream(ByteBuffer buffer) {
45 super();
46 this.buffer = buffer;
47 }
48
49
50
51
52 @Override
53 public int available() {
54 return buffer.remaining();
55 }
56
57
58
59
60 @Override
61 public synchronized void mark(int readlimit) {
62 buffer.mark();
63 }
64
65
66
67
68 @Override
69 public boolean markSupported() {
70 return true;
71 }
72
73
74
75
76 @Override
77 public int read() {
78 if (buffer.hasRemaining()) {
79 return buffer.get() & 0xff;
80 }
81
82 return -1;
83 }
84
85
86
87
88 @Override
89 public int read(byte[] b, int off, int len) {
90 int remaining = buffer.remaining();
91 if (remaining > 0) {
92 int readBytes = Math.min(remaining, len);
93 buffer.get(b, off, readBytes);
94 return readBytes;
95 }
96
97 return -1;
98 }
99
100
101
102
103 @Override
104 public synchronized void reset() {
105 buffer.reset();
106 }
107
108
109
110
111
112
113
114
115
116 @Override
117 public long skip(long n) {
118 int bytes;
119 if (n > Integer.MAX_VALUE) {
120 bytes = buffer.remaining();
121 } else {
122 bytes = Math.min(buffer.remaining(), (int) n);
123 }
124 buffer.position(buffer.position() + bytes);
125
126 return bytes;
127 }
128
129 }