KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > io > MockFileInputStream


1 package com.mockobjects.io;
2
3 import java.io.*;
4
5 import junit.framework.*;
6
7 import com.mockobjects.*;
8
9 public class MockFileInputStream extends FileInputStream {
10     public static final String JavaDoc KNOWN_FILE = "c:\\autoexec.bat";
11
12     private String JavaDoc myContents = "";
13     private int myIndex = 0;
14     
15     private MockFileInputStream() throws FileNotFoundException {
16         // Unfortunately as there is no interface we have to call the supertype,
17
// which requires a file.
18
super(KNOWN_FILE);
19     }
20     
21     public static MockFileInputStream newMockFileInputStream(){
22         try {
23             return new MockFileInputStream();
24         } catch (FileNotFoundException e) {
25             throw new AssertionFailedError("couldn't create MockFileInputStream (requires known file: " + KNOWN_FILE +") "+e);
26         }
27     }
28     
29     public int available() throws IOException {
30         return myContents.length();
31     }
32     public void mark(int readlimit) {
33         MockObject.notYetImplemented("MockFileInputStream.mark");
34     }
35     public int read() throws IOException {
36         if (myIndex == myContents.length())
37             return -1;
38         return myContents.charAt(myIndex++);
39     }
40     public int read(byte b[]) throws IOException {
41         if (myIndex == myContents.length())
42             return -1;
43
44         for (int i = 0; i < myContents.length(); i++) {
45             b[i] = (byte) read();
46         }
47         return myContents.length();
48     }
49     public int read(byte b[], int off, int len) throws IOException {
50         if (myIndex == myContents.length())
51             return -1;
52
53         int bytesRead = 0;
54         for (int i = off; i < len; i++) {
55             b[i] = (byte) myContents.charAt(myIndex);
56             bytesRead++;
57             myIndex++;
58             if (myIndex == myContents.length()) {
59                 break;
60             }
61         }
62         return bytesRead;
63     }
64     public void setupContents(String JavaDoc contents) {
65         myContents = contents;
66         myIndex = 0;
67     }
68     public long skip(long n) throws IOException {
69         MockObject.notYetImplemented("MockFileInputStream.skip");
70         return -1;
71     }
72 }
73
Popular Tags