1 18 package com.Ostermiller.util; 19 20 import java.io.*; 21 22 30 class SizeLimitInputStreamTests { 31 public static void main (String [] args){ 32 try { 33 SizeLimitInputStream slis; 34 35 slis = new SizeLimitInputStream( 36 new ByteArrayInputStream( 37 new byte[] { 38 1,2,3,4 39 } 40 ), 41 3 42 ); 43 if (slis.read() != 1) throw new Exception ("Expected 1"); 44 if (slis.read() != 2) throw new Exception ("Expected 2"); 45 if (slis.read() != 3) throw new Exception ("Expected 3"); 46 if (slis.read() != -1) throw new Exception ("Expected -1"); 47 48 slis = new SizeLimitInputStream( 49 new ByteArrayInputStream( 50 new byte[] { 51 1,2,3,4,5,6,7,8,9 52 } 53 ), 54 6 55 ); 56 if (slis.read() != 1) throw new Exception ("Expected 1"); 57 if (slis.read(new byte[4]) != 4) throw new Exception ("Expected 4 read"); 58 if (slis.read(new byte[4]) != 1) throw new Exception ("Expected 2 read"); 59 if (slis.read() != -1) throw new Exception ("Expected -1"); 60 61 InputStream in = new ByteArrayInputStream( 62 "onetwothreefourfivesixseven".getBytes("ASCII") 63 ); 64 compare("one", readFully(new SizeLimitInputStream(in,3))); 65 compare("", readFully(new SizeLimitInputStream(in,0))); 66 compare("two", readFully(new SizeLimitInputStream(in,3))); 67 compare("three", readFully(new SizeLimitInputStream(in,5))); 68 compare("four", readFully(new SizeLimitInputStream(in,4))); 69 compare("five", readFully(new SizeLimitInputStream(in,4))); 70 compare("six", readFully(new SizeLimitInputStream(in,3))); 71 compare("s", readFully(new SizeLimitInputStream(in,1))); 72 compare("even", readFully(new SizeLimitInputStream(in,4))); 73 74 } catch (Exception x){ 75 System.err.println(x.getMessage()); 76 System.exit(1); 77 } 78 79 } 80 81 private static String readFully(InputStream in) throws IOException { 82 StringBuffer sb = new StringBuffer (); 83 int read; 84 while ((read = in.read()) != -1){ 85 sb.append((char)read); 86 } 87 return sb.toString(); 88 } 89 90 private static void compare(String s1, String s2) throws Exception { 91 if (!s1.equals(s2)) throw new Exception ("Expected " + s1 + " but got " + s2); 92 } 93 94 } 95 | Popular Tags |