001/* 002 * Copyright (c) 1999-2001 Keiron Liddle, Aftex Software 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License as published by the Free Software Foundation; either 007 * version 2.1 of the License, or (at your option) any later version. 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014*/ 015/** 016 * Tester class for BZip streams. 017 */ 018package com.aftexsw.util.bzip; 019 020import org.apache.excalibur.bzip2.*; 021 022import java.io.*; 023import java.lang.reflect.*; 024 025final public class BZipTester { 026 027 public static void main(String args[]) { 028 BZipTester tester = new BZipTester(); 029 tester.runTests(); 030 } 031 032 public BZipTester() {} 033 034 public void runTests() { 035 String fName = "testdata.raw"; 036 long startTime; 037 for(int count = 0; count < 1000;count++) { 038 System.out.println("test:" + count); 039 File file = new File(fName); 040 createRandomData(file); 041 String fcomp = "testdata.bz2"; 042 File compFile = new File(fcomp); 043 int block = (int)(Math.random() * 10) + 1; 044 System.out.println("compressing data:" + block); 045 startTime= System.currentTimeMillis(); 046 compressData(file, compFile, block); 047 System.out.println("time:" + (System.currentTimeMillis() - startTime)); 048 System.out.println("compressed size:" + compFile.length() + " - " + (100 * compFile.length() / (double)file.length()) + "%"); 049 String fout = "testdata.out"; 050 File out = new File(fout); 051 System.out.println("uncompressing data:"); 052 startTime= System.currentTimeMillis(); 053 uncompressData(compFile, out); 054 System.out.println("time:" + (System.currentTimeMillis() - startTime)); 055 if(!compareFiles(file, out)) { 056 System.out.println("files DIFFER"); 057 System.exit(-1); 058 } else { 059 System.out.println("files are same"); 060 } 061 } 062 } 063 064 protected void createRandomData(File out) { 065 try { 066 FileOutputStream outstream = new FileOutputStream(out); 067 BufferedOutputStream bos = new BufferedOutputStream(outstream); 068 int size = (int)(Math.random() * 1024 * 1024 * 1/*0*/); 069 int runs = (int)(Math.random() * 5); 070 System.out.println("creating random data:" + size + " type:" + runs); 071 if(runs == 0) { 072 // runs 073 int pos = 0; 074 while(pos < size) { 075 int runlength = (int)(Math.random() * 1024 * 10); 076 int ch; 077 ch = (int)(Math.random() * 1000); 078 for (int count = 0; count < runlength; count++) { 079 bos.write(ch); 080 pos++; 081 if(pos > size) { 082 break; 083 } 084 } 085 } 086 } else if(runs == 1) { 087 // text - not really since it is still random 088 for (int count = 0; count < size; count++) { 089 int ch; 090 ch = (char)(Math.random() * 32); 091 bos.write(ch); 092 } 093 } else if(runs == 2) { 094 // 1/4 range 095 for (int count = 0; count < size; count++) { 096 int ch; 097 ch = (char)(Math.random() * 64); 098 bos.write(ch); 099 } 100 } else if(runs == 3) { 101 // small runs 102 int pos = 0; 103 while(pos < size) { 104 int runlength = (int)(Math.random() * 10 105 ); 106 int ch; 107 ch = (int)(Math.random() * 32); 108 for (int count = 0; count < runlength; count++) { 109 bos.write(ch); 110 pos++; 111 if(pos > size) { 112 break; 113 } 114 } 115 } 116 } else { 117 // random data 118 for (int count = 0; count < size; count++) { 119 int ch; 120 ch = (int)(Math.random() * 32767 - 65535); 121 bos.write(ch); 122 } 123 } 124 bos.flush(); 125 } catch(IOException e) { 126 e.printStackTrace(); 127 } 128 } 129 130 protected boolean compareFiles(File f1, File f2) { 131 if(f1.length() != f2.length()) { 132 return false; 133 } 134 try { 135 InputStream is1 = new BufferedInputStream(new FileInputStream(f1)); 136 InputStream is2 = new BufferedInputStream(new FileInputStream(f2)); 137 while (true) { 138 int ch1 = is1.read(); 139 int ch2 = is2.read(); 140 if (ch1 == ch2) { 141 if (ch1 == -1) { 142 return true; 143 } 144 } else { 145 return false; 146 } 147 } 148 } catch (Exception e) { 149 e.printStackTrace(); 150 } 151 152 return false; 153 } 154 155 protected void compressData(File in, File out, int block) { 156 try { 157 FileOutputStream outstream = new FileOutputStream(out); 158 BufferedOutputStream bos = new BufferedOutputStream(outstream); 159 bos.write('B'); 160 bos.write('Z'); 161 FileInputStream instream = new FileInputStream(in); 162 BufferedInputStream bis = new BufferedInputStream(instream); 163 int ch = bis.read(); 164 CBZip2OutputStream bzos = new CBZip2OutputStream(bos, block); 165 while(ch != -1) { 166 bzos.write(ch); 167 ch = bis.read(); 168 } 169 bzos.close(); 170 bos.flush(); 171 bos.close(); 172 outstream.flush(); 173 outstream.close(); 174 } catch(IOException e) { 175 e.printStackTrace(); 176 } 177 } 178 179 protected void uncompressData(File in, File out) { 180 try { 181 FileOutputStream outstream = new FileOutputStream(out); 182 FileInputStream instream = new FileInputStream(in); 183 BufferedOutputStream bos = new BufferedOutputStream(outstream); 184 BufferedInputStream bis = new BufferedInputStream(instream); 185 int b = bis.read(); 186 if(b != 'B') 187 return; 188 b = bis.read(); 189 if(b != 'Z') 190 return; 191 CBZip2InputStream bzis = new CBZip2InputStream(bis); 192 int ch = bzis.read(); 193 while(ch != -1) { 194 bos.write(ch); 195 ch = bzis.read(); 196 } 197 bos.flush(); 198 } catch(IOException e) { 199 e.printStackTrace(); 200 } 201 } 202}