1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.mina.util;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.ByteBuffer;
26
27 import static org.junit.Assert.*;
28 import org.junit.Test;
29
30
31
32
33
34
35 public class ByteBufferInputStreamTest {
36 @Test
37 public void testEmpty() throws IOException {
38 ByteBuffer bb = ByteBuffer.allocate(0);
39 InputStream is = new ByteBufferInputStream(bb);
40 assertEquals(-1, is.read());
41 is.close();
42 }
43
44 @Test
45 public void testReadArray() throws IOException {
46 byte[] src = "HELLO MINA".getBytes();
47 byte[] dst = new byte[src.length];
48 ByteBuffer bb = ByteBuffer.wrap(src);
49 InputStream is = new ByteBufferInputStream(bb);
50
51 assertEquals(true, is.markSupported());
52 is.mark(src.length);
53 assertEquals(dst.length, is.read(dst));
54 assertArrayEquals(src, dst);
55 assertEquals(-1, is.read());
56 is.close();
57
58 is.reset();
59 byte[] dstTooBig = new byte[src.length + 1];
60 assertEquals(src.length, is.read(dstTooBig));
61
62 assertEquals(-1, is.read(dstTooBig));
63 }
64
65 @Test
66 public void testSkip() throws IOException {
67 byte[] src = "HELLO MINA!".getBytes();
68 ByteBuffer bb = ByteBuffer.wrap(src);
69 InputStream is = new ByteBufferInputStream(bb);
70 is.skip(6);
71
72 assertEquals(5, is.available());
73 assertEquals('M', is.read());
74 assertEquals('I', is.read());
75 assertEquals('N', is.read());
76 assertEquals('A', is.read());
77
78 is.skip((long) Integer.MAX_VALUE + 1);
79 assertEquals(-1, is.read());
80 is.close();
81 }
82
83 }