Name: rm29839 Date: 02/03/98
/*
java.util.zip.InflaterInputStream.available()
doesn't return the correct number of bytes.
Also, java.util.zip.InflaterInputStream.read(byte[])
doesn't correctly read the file even if passed the
correct number of bytes.
The the test code to work, swing.jar and JFrame.class
must both be in the directory from which you run the
code.
*/
import java.io.*;
import java.util.zip.*;
class JarInput {
static final String JAR_FILE = "swing.jar";
static final String CLASS_FILE = "com/sun/java/swing/JFrame.class";
static final String DOS_FILE = "JFrame.class";
static final String DEBUG_FILE = "debug.txt";
static PrintWriter debugWriter;
public static void main(String[] args) {
try {
ZipFile zipFile = new ZipFile(JAR_FILE);
ZipEntry zipEntry = zipFile.getEntry(CLASS_FILE);
int realFileSize;
//Get InputStream from a class file...
FileInputStream fis = new FileInputStream(DOS_FILE);
//Print the size reported by available() and dump FileInputStream
realFileSize = fis.available();
print("From class file => " + fis + " available=" + realFileSize);
print("Printing the FileInputStream from class file");
print(fis);
//Get InputStream from jar file...
InputStream is = zipFile.getInputStream(zipEntry);
int wrongFileSize = is.available();
byte[] buff = new byte[realFileSize]; //Don't rely on InflaterInputStream.available()
is.read(buff);
ByteArrayInputStream bis = new ByteArrayInputStream(buff);
//Print the size reported by available() and dump ByteArrayInputStream
print("From Jar => " + is + " available=" + wrongFileSize);
print("Printing the ByteArrayInputStream from jar file");
print(bis);
}
catch(IOException ex) {
System.out.println(ex);
}
}
static final void print(InputStream is)
throws IOException
{
int aByte;
StringBuffer strBuf = new StringBuffer();
int colPos = 0;
int colMax = 16;
while((aByte = is.read()) != -1) {
strBuf.append(" " + int2Hex(aByte));
colPos++;
if (colPos == 8) {
strBuf.append(" ");
}
else if(colPos == colMax) {
strBuf.append("\n");
colPos = 0;
}
}
print(strBuf.toString());
}
static String int2Hex(int i) {
int j = i / 16;
int k = i % 16;
return digit2Hex(j) + digit2Hex(k);
}
static String digit2Hex(int i) {
switch(i) {
case 0 : return "0";
case 1 : return "1";
case 2 : return "2";
case 3 : return "3";
case 4 : return "4";
case 5 : return "5";
case 6 : return "6";
case 7 : return "7";
case 8 : return "8";
case 9 : return "9";
case 10 : return "A";
case 11 : return "B";
case 12 : return "C";
case 13 : return "D";
case 14 : return "E";
case 15 : return "F";
default:
throw new RuntimeException(i + " can't be translated to a hex digit");
}
}
static {
try {
debugWriter = new PrintWriter(new FileOutputStream(DEBUG_FILE), true);
}
catch(IOException e) {
System.err.println(DEBUG_FILE + " : " + e);
System.exit(1);
debugWriter = new PrintWriter(System.err, true);
}
}
static final void print(String string) {
debugWriter.println(string);
}
}
(Review ID: 24345)
======================================================================