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 package org.archive.io;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.PrintWriter;
31
32 import org.archive.util.TmpDirTestCase;
33
34 public class RepositionableInputStreamTest extends TmpDirTestCase {
35 private File testFile;
36 private static final String LINE = "0123456789abcdefghijklmnopqrstuv";
37 protected void setUp() throws Exception {
38 super.setUp();
39 this.testFile = new File(getTmpDir(), this.getClass().getName());
40 PrintWriter pw = new PrintWriter(new FileOutputStream(testFile));
41 for (int i = 0; i < 100; i++) {
42 pw.print(LINE);
43 }
44 pw.close();
45 }
46 protected void tearDown() throws Exception {
47 super.tearDown();
48 }
49 public void testname() throws Exception {
50
51 RepositionableInputStream ris =
52 new RepositionableInputStream(new FileInputStream(this.testFile),
53 57);
54 int c = ris.read();
55 assertEquals(1, ris.position());
56 ris.read();
57 ris.position(0);
58 assertEquals(0, ris.position());
59 int c1 = ris.read();
60 assertEquals(c, c1);
61 ris.position(0);
62 byte [] bytes = new byte[LINE.length()];
63 long offset = 0;
64 for (int i = 0; i < 10; i++) {
65 ris.read(bytes, 0, LINE.length());
66 assertEquals(LINE, new String(bytes));
67 offset += LINE.length();
68 assertEquals(offset, ris.position());
69 }
70 long p = ris.position();
71 ris.position(p - LINE.length());
72 assertEquals(p - LINE.length(), ris.position());
73 c = ris.read();
74 assertEquals(c, c1);
75 }
76 }