1 31 package test.pdfbox.filter; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.ByteArrayOutputStream ; 35 import java.io.IOException ; 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 39 import java.util.Collection ; 40 import java.util.Iterator ; 41 42 import junit.framework.Test; 43 import junit.framework.TestCase; 44 import junit.framework.TestSuite; 45 46 import org.pdfbox.cos.COSDictionary; 47 48 import org.pdfbox.filter.DCTFilter; 49 import org.pdfbox.filter.CCITTFaxDecodeFilter; 50 import org.pdfbox.filter.Filter; 51 import org.pdfbox.filter.FilterManager; 52 import org.pdfbox.filter.RunLengthDecodeFilter; 53 54 60 public class TestFilters extends TestCase 61 { 62 private static final int BUFFER_SIZE = 2048; 63 private static final COSDictionary EMPTY_DICTIONARY = new COSDictionary(); 64 65 70 public TestFilters( String name ) 71 { 72 super( name ); 73 } 74 75 80 public static Test suite() 81 { 82 return new TestSuite( TestFilters.class ); 83 } 84 85 90 public void testFilters() throws IOException 91 { 92 FilterManager manager = new FilterManager(); 93 Collection filters = manager.getFilters(); 94 95 Iterator filterIter = filters.iterator(); 96 while( filterIter.hasNext() ) 97 { 98 long start = System.currentTimeMillis(); 99 Filter filter = (Filter)filterIter.next(); 100 if( !(filter instanceof DCTFilter || 101 filter instanceof CCITTFaxDecodeFilter || 102 filter instanceof RunLengthDecodeFilter)) 103 { 104 checkFilter( new File ( "classes" ), filter ); 105 long stop = System.currentTimeMillis(); 106 System.out.println( "Time for filter " + filter.getClass().getName() + "=" + (stop-start) ); 107 } 108 } 109 } 110 111 119 private void checkFilter( File file, Filter filter ) throws IOException 120 { 121 if( file.isDirectory() ) 122 { 123 File [] subFiles = file.listFiles(); 124 for( int i=0; i<subFiles.length; i++ ) 125 { 126 checkFilter( subFiles[i], filter ); 127 } 128 } 129 else 130 { 131 ByteArrayOutputStream output = new ByteArrayOutputStream (); 132 133 ByteArrayOutputStream encoded = new ByteArrayOutputStream (); 134 ByteArrayOutputStream decoded = new ByteArrayOutputStream (); 135 FileInputStream fin = new FileInputStream ( file ); 136 int amountRead = 0; 137 byte[] buffer = new byte[ BUFFER_SIZE ]; 138 while( (amountRead = fin.read( buffer, 0, BUFFER_SIZE )) != -1 ) 139 { 140 output.write( buffer, 0, amountRead ); 141 } 142 fin.close(); 143 byte[] original = output.toByteArray(); 144 filter.encode( new ByteArrayInputStream ( original ), encoded, EMPTY_DICTIONARY ); 145 filter.decode( new ByteArrayInputStream ( encoded.toByteArray() ), decoded, EMPTY_DICTIONARY ); 146 147 cmpArray( original, decoded.toByteArray(), filter, file ); 148 } 149 } 150 151 159 private void cmpArray( byte[] firstArray, byte[] secondArray, Filter filter, File file ) 160 { 161 String fileMsg = filter.getClass().getName() + " " + file.getAbsolutePath(); 162 if( firstArray.length != secondArray.length ) 163 { 164 fail( "The array lengths do not match for " + fileMsg + 165 ", firstArray length was: " + firstArray.length + 166 ", secondArray length was: " + secondArray.length); 167 } 168 169 for( int i=0; i<firstArray.length; i++ ) 170 { 171 if( firstArray[i] != secondArray[i] ) 172 { 173 fail( "Array data does not match " + fileMsg ); 174 } 175 } 176 } 177 } | Popular Tags |