GZIPInputStream

hi everyone,
I'm having problems with GZIPInputStream. I wanted to send compressed data from the server to a client, using UDP, so I gziped data before sending it.
The problem is on the client side. I'm using this code to uncompress the data:
DatagramSocket ds = new DatagramSocket(55555);
DatagramPacket packet = new DatagramPacket(new byte[2048], 2048);
while (running) {
ds.receive(packet);
byte[] b = new byte[ packet.getLength() ];
System.arraycopy(packet.getData(), 0, b, 0, b.length);
ByteArrayInputStream bin = new ByteArrayInputStream(b, 0, b.length);
GZIPInputStream gin = new GZIPInputStream( bin );
StringBuffer sb = new StringBuffer(128);
byte bt;
while ( (bt = (byte) gin.read())!=-1 ) { <=== EXCEPTION!
sb.append( (char) bt );
System.out.println(sb.toString());
This code throws the following Exception:
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:211)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:133)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:86)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:104)
Does anyone have an idea?
thanks a lot for your help!
Luis Javier

Without seeing the code you used to gzip your data in the first place, my best offering is that you need to call the finish() method of the GZIPOutputStream once you've finished writing your data out. In fact, you should probably flush your stream(s) before calling finish().
Hope this helps...
Shaun

Similar Messages

  • How to decompress gzip file with GZIPInputStream

    Hello,
    i have written a sample which decompress a gzip file but the decompressed file is not equivalent to the original (length different and probably the content!).
    Here is my code, can u help me where is the problem?
    public void deGZip(String fileName) throws IOException, FileNotFoundException
              File zipFile = new File(fileName); // like "xxx.xx.gz"
              FileInputStream in = new FileInputStream(zipFile);
              GZIPInputStream gzipIn = new GZIPInputStream(new BufferedInputStream(in));
              int zipFileLength = new Long(zipFile.length()).intValue();
              String nom = zipFile.getName().substring(0,zipFile.getName().length()-3); //like "xxx.xx"
              File decompressedFile = new File(nom);
              FileOutputStream out = new FileOutputStream(decompressedFile);
              BufferedOutputStream dest = new BufferedOutputStream(out);
              byte[] buf = new byte[zipFileLength];
              int i = 0;
              while ((i = in.read(buf,0,1500000)) >= 0)
              {   dest.write(buf,0,i);  
              dest.flush();
              dest.close();
              gzipIn.close();
         }

    The main problem was that you were reading from "in" instead of "gzipIn", so you were writing the compressed data to the output.
    Also, you were allocating buf to be the size of the whole file, and then using read() passing in some big number. If the file size is less than that big number, then it crashed.
    import java.io.*;
    import java.util.zip.GZIPInputStream;
    public class GUnzip {
        public static void main(String[] args) throws IOException {
            deGZip(args[0]);
        public static void deGZip(String fileName) throws IOException, FileNotFoundException {
            File zipFile = new File(fileName); // like "xxx.xx.gz"
            FileInputStream in = new FileInputStream(zipFile);
            GZIPInputStream gzipIn = new GZIPInputStream(new BufferedInputStream(in));
            String nom = zipFile.getName().substring(0, zipFile.getName().length() - 3); //like "xxx.xx"
            File decompressedFile = new File(nom);
            FileOutputStream out = new FileOutputStream(decompressedFile);
            BufferedOutputStream dest = new BufferedOutputStream(out);
            byte[] buf = new byte[8 * 1024];  // FIXED: this is independent of the file size
            int i;
            while ((i = gzipIn.read(buf)) >= 0) {  // FIXED: use gzipIn, use the whole buffer
                dest.write(buf, 0, i);
            dest.flush();
            dest.close();
            gzipIn.close();

  • Problem when uncompressing using GZIPInputStream

    i uploaded a 2.7MB gzipped byte array into MySQL database. The original file size before compressing was 9.3MB. When I retrieve the file from the database, everything works fine, but after uncompressing it using GZIP, it's only 9.2MB. I compared both the files, both look same in the beginning, but the last bytes are missing in the retrieved file. Here is my code.Can somebody give me some insight?
    byte[] bytes=getFile();
    ByteArrayInputStream bStream=new ByteArrayInputStream(bytes);
    GZIPInputStream iStream=new GZIPInputStream(bStream);
    File file = new File("/home/antn/file/testfile.cel");
    OutputStream oStream=new FileOutputStream(file);
    byte [] buf = new byte[512];
    int len = 0;
    while ( (len=iStream.read(buf))!= -1 )
    oStream.write(buf,0,len);
    iStream.close();
    oStream.close();
    bStream.close();
    Also I am getting this exception whenever I run the program.
    Exception in thread "main" java.io.EOFException: Unexpected end of ZLIB input stream
         at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:216)
         at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:134)
         at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:87)
         at java.io.FilterInputStream.read(FilterInputStream.java:90)
    at service.RetrieveFile.main(RetrieveFile.java:93)

    Hey guys,
    Thanks for your help. The problem was in writing to the database. Eventhough I closed all the input and output streams, I didn't flush the output stream and close it immediately. So now its working and thanks again for the great help.

  • GZIPInputStream non-blocking

    Hello!
    I'm using Java NIO for a network app. I need to decompress GZIP compressed data and I'm wondering if I can do that in a non-blocking way to fit my architecture. As it is now, using PipedInputStream with GZIPInputStream will block until enough data is available to decompress anything. I would like it to return empty handed if there's not enough data yet. Any solution to this?
    //shadewind

    You need a way to send the length of the GZIP file with the data so the reader know how much data to read.

  • How to monitor place in GzipInputStream

    My app is processing a large .gz file using GZIPInputStream. I want to use a progressBar to monitor the processing of the file. How can I tell the current position in the inputStream, in order to work out the % done (i.e. (currentPostiton / fileLength) * 100 )?

    Put a counting wrapper around either the GZIPInputStream or around the underlying InputStream.

  • Using GZIPInputStream on Unix

    I'm using GZIPInputStream in a Windows environment, but will soon move the code to a Unix machine. I'm assuming that there will be no problem here, but one function in the source code concerns me:
    readUShort(), which has the comment "Reads unsigned integer in Intel byte order."
    Before I break my head figuring out if there's a conflict between the Intel/non-Intel byte orders, does anyone know if this function will function exactly the same on the different platforms?
    I'm particularly interested in this function because I mimic its functionality to detect the start of a GZIP stream by searching for the GZIP "magic" bytes. My only concern is that somehow the byte order will be switched and I won't be able to detect those bytes on a Unix machine.

    does anyone know if this function will function exactly the same on the different platforms? Yes it will. The low-order byte is read first, then the high-order byte, then the result is formed.

  • GZIPInputStream and number of read bytes

    Hi all,
    I have a GZIPInputStream wrapped up in BufferedReader and would like to determine either the number of bytes read or the number of bytes left to read.
    GZIPInputStream provides the available() function for determining the number of bytes left to read. However, this returns always one (http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilterInputStream.html#available()).
    The available() function of the FileInputStream returns always the length of the file in bytes.
    Does anybody have an idea how to do this?
    Thanks,
    Andreas

    I have a GZIPInputStream wrapped up inBufferedReader
    and would like to determine either the number of
    bytes read or the number of bytes left to read.
    GZIPInputStream provides the available() functionfor
    determining the number of bytes left to read.No! It provides the number of bytes that can be read
    without blocking which may be much less that the
    number of bytes left to read and can be zero with
    bytes still to be read.That's true. I was aware of this, but not clear enough.
    However, this returns always one
    (http://java.sun.com/j2se/1.5.0/docs/api/java/io/Filte
    rInputStream.html#available()).
    The available() function of the FileInputStream
    returns always the length of the file in bytes.No! It provides the number of bytes that can be read
    without blocking. It cannot return the length of the
    file since a file length can be greater than the
    maximum integer.Again, that's true, but in my case it always returns the same number.
    If I read a simple text file it works perfectly.
    I want a way to determine approximately where in the file I am. Is there some way to do this for zipped files?

  • GZIPInputStream Extremely Slow

    I've got some code that uses a LineNumberReader, a subclass of BufferedReader, to read text files, and it's pretty fast. However, when I try to extend it to read files in GZIP format it's extremely slow (100 times slower at least).
    Here's a code snippet:
      private void openFile() throws FileNotFoundException{
        if (filename.substring(filename.length() - 3,
                      filename.length()).equals(".gz")){
          try{
         file =
           new LineNumberReader(new InputStreamReader
             (new BufferedInputStream(new GZIPInputStream(new FileInputStream(filename)))));
          catch (IOException ioe){
         throw new FileNotFoundException(ioe.getClass().getName() + ": "
                             + ioe.getMessage());
        else{
          file = new LineNumberReader(new FileReader(filename));
        lineNumber = 0;
      }It doesn't seem to make any difference whether I wrap the BufferedInputStream around the GZIPInputStream or not, and LineNumberReader itself is also supposed to be buffered. Any ideas? I'm using the default buffer size (whatever that is).

    new LineNumberReader(new InputStreamReader
             (new BufferedInputStream(new GZIPInputStream(new FileInputStream(filename)))));Yeah, this construct pretty much sucks. The buffering occurs in LineNumberReader so don't bother with the other BufferedInputStream.
    Don't catch a general IOException and wrap it in a FileNotFoundException, thats nonsense.
    A quick microbenchmark on file composed of random character data showed anywhere from a 25 to 50% additional overhead on gzip reading.

  • GZIPInputStream returning EOF after only 62k read

    Hi,
    I'm trying to get a darn gzip file read, and it ends after 62k. I've tried many combinations of streams and readers, all the way down to just using a GZIPInputStream(new FileInputStream(filename)) and reading byte by byte.
    I keep running into '-1' after about 62k bytes have been read. I'm assuming it's because a buffer somewhere ends at 62k, but I've tried setting the buffer to a larger size with the same result.
    I want to read line by line...
    package test;
    import java.io.InputStreamReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.LineNumberReader;
    import java.util.zip.GZIPInputStream;
    public class GZipReadTest2 {
              private static final String FILE = "myfile.gz";
              private static LineNumberReader lnr;
              public static void main(String[] args) throws FileNotFoundException, IOException {
                   lnr = new LineNumberReader(
                             new InputStreamReader(
                             new GZIPInputStream(
                             new FileInputStream(FILE))));
                   String line = "start";
                   while((line = lnr.readLine()) != null) {
                        try {
                             lnr.readLine();
                        } catch (IOException e) {
                             e.printStackTrace();
                        System.out.println(line);
                   System.out.println("Done reading " + FILE + " at line: " + lnr.getLineNumber());
         }

    I think its because these logs are written by syslog in gzipped batches, so each chunk has an eof, even though there is more data in the file. Does anyone know how to ignore or check for this ?

  • Zip data posted from client does not show up correctely at Server

    The java client post GZip'd data to the Webserver. The webserver for some reason shows the first 10 bytes correctly. Not sure what I am overlooking
    Also get java.io.EOFException: Unexpected end of ZLIB input stream
    public class GetPost {
         public String line;
         public String inputLine;
    public static void main(String[] args) throws Exception {
              GetPost r = new GetPost();
                   r.postURL("User","pass");
    // public Reverse { }
    public static byte [] zip(String data)
    throws IOException
    byte[] incomingBytes = data.getBytes();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream zip = new GZIPOutputStream(baos);
    for (int i = 0; i < incomingBytes.length; i++)
    zip.write(incomingBytes[i] & 0xFF);
    zip.close();
    byte[] uncompressedBytes = baos.toByteArray();
    return uncompressedBytes;
    public static String unzip(byte [] dataBytes)
    throws IOException
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bios = new ByteArrayInputStream(dataBytes);
    GZIPInputStream unzip = new GZIPInputStream(bios);
    int in = unzip.read();
    while (in != -1)
    baos.write(in);
    in = unzip.read();
    unzip.close();
    return new String(baos.toByteArray());
    public String postURL( String t1 , String t2 ){
    try {
              String data1="h";
    URL urlpost = new URL("http://192.168.15.4:8080/result.html");
         URLConnection conn = urlpost.openConnection();
         conn.setDoOutput(true);
         GZIPOutputStream gz = new GZIPOutputStream(conn.getOutputStream());
         byte [] kkkk=zip(data1);
         String uuu=unzip(kkkk);
         System.out.println("XXXXX" + uuu );
         for (int i=0; i< kkkk.length ; i++){
              Byte jj= new Byte(kkkk);
              System.out.println("LLLL " + " " + i + " " + jj );
         int t= kkkk.length;
         System.out.println("IIII" + t);
         int x= data1.getBytes().length;
         gz.write(kkkk);
         gz.finish();
         gz.close();
         // Get the response
         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
         // Process line...
         System.out.println ("The line is " + line );
         } catch (Exception e) {
              return line;
    The client output is
    LLLL 0 31
    LLLL 1 -117
    LLLL 2 8
    LLLL 3 0
    LLLL 4 0
    LLLL 5 0
    LLLL 6 0
    LLLL 7 0
    LLLL 8 0
    LLLL 9 0
    LLLL 10 -53
    LLLL 11 0
    LLLL 12 0
    LLLL 13 -25
    LLLL 14 6
    LLLL 15 107
    LLLL 16 -111
    LLLL 17 1
    LLLL 18 0
    LLLL 19 0
    LLLL 20 0
    Server output is correct only for the first 10 bytes
    LLLL 0 31
    LLLL 1 -117
    LLLL 2 8
    LLLL 3 0
    LLLL 4 0
    LLLL 5 0
    LLLL 6 0
    LLLL 7 0
    LLLL 8 0
    LLLL 9 0
    LLLL 10 -53
    LLLL 11 0
    LLLL 12 0
    LLLL 13 -25
    LLLL 14 6
    LLLL 15 107
    LLLL 16 -111
    LLLL 17 1
    LLLL 18 0
    LLLL 19 0
    LLLL 20 0

    FYI - I got it to work ... I was gzip it twice... here is what worked
    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.util.zip.*;
    import java.io.OutputStreamWriter;
    public class WorkingCopyOfGetPost {
         public String line;
         public String inputLine;
    public static void main(String[] args) throws Exception {
              WorkingCopyOfGetPost r = new WorkingCopyOfGetPost();
                   r.postURL("User","pass");
    // public Reverse { }
    public static byte [] zip(String data) throws IOException {
         byte[] incomingBytes = data.getBytes();
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         GZIPOutputStream zip = new GZIPOutputStream(baos);
         for (int i = 0; i < incomingBytes.length; i++){
              zip.write(incomingBytes[i] & 0xFF);
         zip.close();
         byte[] uncompressedBytes = baos.toByteArray();
         return uncompressedBytes;
    public static String unzip(byte [] dataBytes) throws IOException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ByteArrayInputStream bios = new ByteArrayInputStream(dataBytes);
         GZIPInputStream unzip = new GZIPInputStream(bios);
         int in = unzip.read();
         while (in != -1){
              baos.write(in);
              in = unzip.read();
         unzip.close();
         return new String(baos.toByteArray());
    public static ByteArrayOutputStream zip1(String data) throws IOException {
              byte[] incomingBytes = data.getBytes();
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              GZIPOutputStream zip = new GZIPOutputStream(baos);
              for (int i = 0; i < incomingBytes.length; i++){
                   zip.write(incomingBytes[i] & 0xFF);
              zip.close();
              //byte[] uncompressedBytes = baos.toByteArray();
              return baos;
    public String postURL( String t1 , String t2 ){
         String data1="t";
         try {
              /* byte [] kkkk=zip(data1);
              for (int x=0;x<20;x++){
                   System.out.println("Byte Array Values " + x + kkkk[x] );
              ByteArrayOutputStream bo= zip1(data1);
              byte [] AAA=bo.toByteArray();
              for (int x=0;x<21;x++){
                   System.out.println("Byte Array Values " + x + " "+ AAA[x] );
              URL urlpost = new URL("http://192.168.15.4:8080/result.html");
         URLConnection conn = urlpost.openConnection();
         conn.setDoOutput(true);
         System.out.println( "The size of the byteArrayOutputstream " + bo.size() );
         bo.writeTo(conn.getOutputStream());
         // GZIPOutputStream gz = new GZIPOutputStream(conn.getOutputStream());
         // System.out.println("rrrr " + kkkk.length);
         // gz.write(kkkk);
         //gz.finish();
         //gz.flush();
         bo.flush();
         // String s=unzip(kkkk) ;
         //System.out.println("s " + s);
         // Get the response
         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
         // Process line...
         System.out.println ("The line is " + line );
         rd.close();
         bo.close();
         } catch (Exception e) {
              return line;
    }

  • Problem in using socket streams with encryption and decryption

    Hi,
    I am developing a client/server program with encryption and decryption at both end. While sending a message from client it should be encrypted and at the receiving end(server) it should be decrypted and vice versa.
    But while doing so i got a problem if i use both encryption and decryption at both ends. But If i use only encryption at one (only outputstream) and decryption at other end(only inputstream) there is no problem.
    Here is client/server pair of programs in which i am encrypting the outputstream of the socket in client side and decrypting the inputstream of the socket in server side.
    serverSocketDemo.java
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.util.*;
    import java.util.zip.*;
    class serverSocketDemo
         public static void main(String args[])
              try
              {                    //server listening on port 2000
                   ServerSocket server=new ServerSocket(2000);
                   while (true)
                        Socket theConnection=server.accept();
                        System.out.println("Connecting from local address : "+theConnection.getLocalAddress());
                        System.out.println("Connection request from : "+theConnection.getInetAddress());
                        //Input starts from here
                        Reader in=new InputStreamReader(getNetInStream(theConnection.getInputStream()),"ASCII");
                        StringBuffer strbuf=new StringBuffer();
                        int c;
                        while (true)
                             c=in.read();
                             if(c=='\n' || c==-1)
                                  break;
                             strbuf.append((char)c);     
                        String str=strbuf.toString();
                        System.out.println("Message from Client : "+str);
                        in.close();               
                        theConnection.close();
              catch(BindException e)
                   System.out.println("The Port is in use or u have no privilage on this port");
              catch(ConnectException e)
                   System.out.println("Connection is refused at remote host because the host is busy or no process is listening on that port");
              catch(IOException e)
                   System.out.println("Connection disconnected");          
              catch(Exception e)
         public static BufferedInputStream getNetInStream(InputStream in) throws Exception
              // register the provider that implements the algorithm
              Provider sunJce = new com.sun.crypto.provider.SunJCE( );
              Security.addProvider(sunJce);
              // create a key
              byte[] desKeyDataDec = "This encryption can not be decrypted".getBytes();
              DESKeySpec desKeySpecDec = new DESKeySpec(desKeyDataDec);
              SecretKeyFactory keyFactoryDec = SecretKeyFactory.getInstance("DES");
              SecretKey desKeyDec = keyFactoryDec.generateSecret(desKeySpecDec);
              // use Data Encryption Standard
              Cipher desDec = Cipher.getInstance("DES");
              desDec.init(Cipher.DECRYPT_MODE, desKeyDec);
              CipherInputStream cin = new CipherInputStream(in, desDec);
              BufferedInputStream bin=new BufferedInputStream(new GZIPInputStream(cin));
              return bin;
    clientSocketDemo.java
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.util.*;
    import java.util.zip.*;
    class clientSocketDemo
         public static void main(String args[])
              try
                   Socket theConnection=new Socket("localhost",2000);
                   System.out.println("Connecting from local address : "+theConnection.getLocalAddress());
                   System.out.println("Connecting to : "+theConnection.getInetAddress());
                   //Output starts from here               
                   OutputStream out=getNetOutStream(theConnection.getOutputStream());
                   out.write("Please Welcome me\n".getBytes());
                   out.flush();
                   out.close();
                   theConnection.close();
              catch(BindException e)
                   System.out.println("The Port is in use or u have no privilage on this port");
              catch(ConnectException e)
                   System.out.println("Connection is refused at remote host because the host is busy or no process is listening on that port");
              catch(IOException e)
                   System.out.println("Connection disconnected");          
              catch(Exception e)
         public static OutputStream getNetOutStream(OutputStream out) throws Exception
              // register the provider that implements the algorithm
              Provider sunJce = new com.sun.crypto.provider.SunJCE( );
              Security.addProvider(sunJce);
              // create a key
              byte[] desKeyDataEnc = "This encryption can not be decrypted".getBytes();
              DESKeySpec desKeySpecEnc = new DESKeySpec(desKeyDataEnc);
              SecretKeyFactory keyFactoryEnc = SecretKeyFactory.getInstance("DES");
              SecretKey desKeyEnc = keyFactoryEnc.generateSecret(desKeySpecEnc);
              // use Data Encryption Standard
              Cipher desEnc = Cipher.getInstance("DES");
              desEnc.init(Cipher.ENCRYPT_MODE, desKeyEnc);
              CipherOutputStream cout = new CipherOutputStream(out, desEnc);
              OutputStream outstream=new BufferedOutputStream(new GZIPOutputStream(cout));
              return outstream;
    Here is client/server pair in which i use both encrypting outpustream and decrypting inputstream at both ends.
    serverSocketDemo.java
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.util.*;
    import java.util.zip.*;
    class serverSocketDemo
         private Cipher desEnc,desDec;
         serverSocketDemo()
              try
                   // register the provider that implements the algorithm
                   Provider sunJce = new com.sun.crypto.provider.SunJCE( );
                   Security.addProvider(sunJce);
                   // create a key
                   byte[] desKeyData = "This encryption can not be decrypted".getBytes();
                   DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
                   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
                   SecretKey desKey = keyFactory.generateSecret(desKeySpec);
                   desEnc = Cipher.getInstance("DES");
                   desEnc.init(Cipher.ENCRYPT_MODE, desKey);
                   desDec = Cipher.getInstance("DES");
                   desDec.init(Cipher.DECRYPT_MODE, desKey);               
              catch (javax.crypto.NoSuchPaddingException e)
                   System.out.println(e);          
              catch (java.security.NoSuchAlgorithmException e)
                   System.out.println(e);          
              catch (java.security.InvalidKeyException e)
                   System.out.println(e);          
              catch(Exception e)
                   System.out.println(e);
              startProcess();
         public void startProcess()
              try
                   ServerSocket server=new ServerSocket(2000);
                   while (true)
                        final Socket theConnection=server.accept();
                        System.out.println("Connecting from local address : "+theConnection.getLocalAddress());
                        System.out.println("Connection request from : "+theConnection.getInetAddress());
                        Thread input=new Thread()
                             public void run()
                                  try
                                       //Input starts from here
                                       Reader in=new InputStreamReader(new BufferedInputStream(new CipherInputStream(theConnection.getInputStream(), desDec)),"ASCII");
                                       StringBuffer strbuf=new StringBuffer();
                                       int c;
                                       while (true)
                                            c=in.read();
                                            if(c=='\n'|| c==-1)
                                                 break;
                                            strbuf.append((char)c);     
                                       String str=strbuf.toString();
                                       System.out.println("Message from Client : "+str);
                                  catch(Exception e)
                                       System.out.println("Error caught inside input Thread : "+e);
                        input.start();
                        Thread output=new Thread()
                             public void run()
                                  try
                                       //Output starts from here
                                       OutputStream out=new BufferedOutputStream(new CipherOutputStream(theConnection.getOutputStream(), desEnc));
                                       System.out.println("it will not be printed");
                                       out.write("You are Welcome\n".getBytes());
                                       out.flush();
                                  catch(Exception e)
                                       System.out.println("Error caught inside output Thread : "+e);
                        output.start();
                        try
                             output.join();
                             input.join();
                        catch(Exception e)
                        theConnection.close();
              catch(BindException e)
                   System.out.println("The Port is in use or u have no privilage on this port");
              catch(ConnectException e)
                   System.out.println("Connection is refused at remote host because the host is busy or no process is listening on that port");
              catch(IOException e)
                   System.out.println("Connection disconnected");          
              catch(Exception e)
         public static void main(String args[])
              serverSocketDemo server=new serverSocketDemo();          
    clientSocketDemo.java
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.util.*;
    import java.util.zip.*;
    class clientSocketDemo
         private Cipher desEnc,desDec;
         clientSocketDemo()
              try
                   // register the provider that implements the algorithm
                   Provider sunJce = new com.sun.crypto.provider.SunJCE( );
                   Security.addProvider(sunJce);
                   // create a key
                   byte[] desKeyData = "This encryption can not be decrypted".getBytes();
                   DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
                   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
                   SecretKey desKey = keyFactory.generateSecret(desKeySpec);
                   desEnc = Cipher.getInstance("DES");
                   desDec = Cipher.getInstance("DES");
                   desEnc.init(Cipher.ENCRYPT_MODE, desKey);
                   desDec.init(Cipher.DECRYPT_MODE, desKey);               
              catch (javax.crypto.NoSuchPaddingException e)
                   System.out.println(e);          
              catch (java.security.NoSuchAlgorithmException e)
                   System.out.println(e);          
              catch (java.security.InvalidKeyException e)
                   System.out.println(e);          
              catch(Exception e)
                   System.out.println(e);
              startProcess();
         public void startProcess()
              try
                   final Socket theConnection=new Socket("localhost",2000);
                   System.out.println("Connecting from local address : "+theConnection.getLocalAddress());
                   System.out.println("Connecting to : "+theConnection.getInetAddress());
                   Thread output=new Thread()
                        public void run()
                             try
                                  //Output starts from here               
                                  OutputStream out=new BufferedOutputStream(new CipherOutputStream(theConnection.getOutputStream(), desEnc));
                                  out.write("Please Welcome me\n".getBytes());
                                  out.flush();
                             catch(Exception e)
                                  System.out.println("Error caught inside output thread : "+e);
                   output.start();     
                   Thread input=new Thread()
                        public void run()
                             try
                                  //Input starts from here
                                  Reader in=new InputStreamReader(new BufferedInputStream(new CipherInputStream(theConnection.getInputStream(), desDec)),"ASCII");          
                                  System.out.println("it will not be printed");
                                  StringBuffer strbuf=new StringBuffer();
                                  int c;
                                  while (true)
                                       c=in.read();
                                       if(c=='\n' || c==-1)
                                            break;
                                       strbuf.append((char)c);     
                                  String str=strbuf.toString();
                                  System.out.println("Message from Server : "+str);
                             catch(Exception e)
                                  System.out.println("Error caught inside input Thread : "+e);
                   input.start();
                   try
                        output.join();
                        input.join();
                   catch(Exception e)
                   theConnection.close();
              catch(BindException e)
                   System.out.println("The Port is in use or u have no privilage on this port");
              catch(ConnectException e)
                   System.out.println("Connection is refused at remote host because the host is busy or no process is listening on that port");
              catch(IOException e)
                   System.out.println("Connection disconnected");          
              catch(Exception e)
         public static void main(String args[])
              clientSocketDemo client=new clientSocketDemo();     
    **** I know that the CInput tries to read some header stuff thats why i used two threads for input and output.
    Waiting for the reply.
    Thank you.

    Do not ever post your code unless requested to. It is very annoying.
    Try testing what key is being used. Just to test this out, build a copy of your program and loop the input and outputs together. Have them print the data stream onto the screen or a text file. Compare the 1st Output and the 2nd Output and the 1st Input with the 2nd Input and then do a static test of the chipher with sample data (same data which was outputted), then do another cipher test with the ciphertext created by the first test.
    Everything should match - if it does not then follow the steps below.
    Case 1: IO Loops do not match
    Case 2: IO Loops match, but ciphertext 1st run does not match loop
    Case 3: IO Loops match, 1st ciphertext 1st run matches, but 2nd run does not
    Case 4: IO Loops match, both chiphertext runs do not match anything
    Case 5: Ciphertext runs do not match eachother when decrypted correctly (outside of the test program)
    Problems associated with the cases above:
    Case 1: Private Key is changing on either side (likely the sender - output channel)
    Case 2: Public Key is changing on either side (likely the sender - output channel)
    Case 3: Private Key changed on receiver - input channel
    Case 4: PKI failure, causing private key and public key mismatch only after a good combination was used
    Case 5: Same as Case 4

  • "Invalid Block Type" exception message

    I got "Invalid Block Type" exception message in my program. I'm not sure what this is. Could somebody please advice?
    Thanks
    GZIPInputStream gzip_in_stream = null;
    BufferedOutputStream destination_out_stream = new BufferedOutputStream(
       new FileOutputStream(weatherFile), BUF_SIZE);
       byte[] input_buffer = new byte[BUF_SIZE];
    int byteLength = 0;          
    while ( (byteLength = gzip_in_stream.read(input_buffer, 0, BUF_SIZE)) > 0 )
    destination_out_stream.write(input_buffer, 0, byteLength);     
    destination_out_stream.flush(); // Ensure all the data is written to the output.
    destination_out_stream.close();
    gzip_in_stream.close();     

    ZipException, and it only says "Signals that a Zip exception of some sort has occurred."
    I forgot to include this.
    InputStream tempStream = ftpClient.retrieveFileStream(zippedFile);
    gzip_in_stream = new GZIPInputStream(
    new BufferedInputStream(tempStream));

  • Help with Kevin Bacon game

    I would really appreciate some help. Yes this is an assignment who in his crazy mind would try to do this just to learn or practice their java. I have some code already done but I ran out of memory. The other thing my code works like the one at Virginia University. I'm awarding 10 dukes I can award another 10 dukes. I'll let you know later, thanks in advance.
    This is the code I have:
    import java.io.*;
    import java.util.zip.*;
    import java.util.ArrayList;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Set;
    import java.util.Iterator;
    * A class that download files , create Maps and search for information with
    * in the maps, giving an output of the actor that share more movies with the
    * input actor that the user gives
    public class DataBase
      private Map map1 = new HashMap( );  // actor
      private Map map2= new HashMap( );   //movies
      private int okactor = 0;
    // Main Method
      public static void main (String args [])throws IOException
        DataBase testing = new DataBase();
        System.out.println("Program was run in Pentium 4 in AUL");
        System.out.println("Starting Time : " + System.currentTimeMillis());
        testing.loadFile("C:\\My Documents\\FIU\\COP3530_Data_Strutures\\program5\\actresses.list.gz");   
        testing.loadFile("C:\\My Documents\\FIU\\COP3530_Data_Strutures\\program5\\actors.list.gz");
        //testing.loadFile("\\\\Cougar\\cop3530\\actresses.list.gz");   
        //testing.loadFile("\\\\Cougar\\cop3530\\actors.list.gz");
        System.out.println(" Ending Time of Downloading:  " + System.currentTimeMillis());
        int infiniteloop = 0 ;
           while (infiniteloop == 0)
                     System.out.println("Number of Actors and Actresses : " + testing.getActorCount());
                  System.out.println("Number of Movies: " + testing.getMovieCount());  
                      System.out.println("");
                      System.out.println("Enter a name please");
                      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                      String name = in.readLine(); 
                      testing.computeSharedMovies(name);
                  List listofnames = new ArrayList();
                  listofnames =  testing.mostSharedMovies();  
                  testing.print(name, listofnames);
    //static class actor 
      private static final class Actor
         String name;
         int    data;  // number of shared movies,
                       // determined by computeSharedMovies
         public String toString( )
           { return name; }
         public int hashCode( )
           { return name.hashCode( ); }
         public boolean equals( Object other )
           { return (other instanceof Actor) &&
                    ( (Actor) other ).name.equals( name );
    *Loads the two files files
    *@param String is the fileName to be download
      public void loadFile( String fileName ) throws IOException
           BufferedReader x = new BufferedReader ( new InputStreamReader(new GZIPInputStream (new BufferedInputStream( new FileInputStream(fileName)))));
                String line;
          int start = 0 ;
          ArrayList actorList = new ArrayList();
          ArrayList movies = new ArrayList();
          Actor key = new Actor();
          int p = 0;    //parameters
          int p2 =0 ;
          String you = null ;
          String year = null;
          String trimMovie = null;
          int par1 = 0;    //parameter
          int par2 =0;
          String addingmovie = null;
            while((line = x.readLine()) != null)
                if(line.indexOf("Name") == 0 )
                      start++;
                if(start == 0)
                continue;
                  if( start >= 1)
                                  if(line.indexOf("-----------------------") == 0)
                                          break;
                                  if(((line.trim()).length()) == 0)
                                         continue;
                                  else if(line.indexOf("----") == 0)
                                          continue;
                                     else if (line.indexOf("Name") == 0)
                                             continue;
                                  else if(line.indexOf("\t") != 0)
                                     p  = line.indexOf("\t");
                                     p2 = line.lastIndexOf(")");
                                     String actor = (line.substring(0,p));
                                       key = new Actor();
                                       key.name = actor;                              
                                        you = (line.substring(p, (p2 + 1)));
                                          if (you.indexOf("(TV)") > 0)
                                                         continue;                                                                                                
                                       p = you.indexOf("\t");
                                       p2 = you.indexOf(")");
                                      you = (you.substring(p, p2 +1)).trim();                                 
                                            if(you.indexOf("\"") == 0)
                                                    continue;                                                                                        
                                            year = you ;
                                       p = year.indexOf("(");
                                       p2 = year.indexOf(")");
                                       year = year.substring(p + 1 , p2);                                 
                                            if ( ( ((Comparable)year).compareTo("2002") ) >= 0)
                                                    continue;                                                                                         
                                     you = you.intern();                                                                  
                                     movies = new ArrayList();
                                      movies.add(you);
                                     movies.trimToSize() ;                    
                                        map1.put(key , movies);
                                           if(map2.containsKey(you))
                                                      ((ArrayList)map2.get(you)).add(key) ;
                                            else
                                                         actorList = new ArrayList();
                                                           actorList.add(key);
                                                           actorList.trimToSize() ;                                                          
                                                           map2.put(you, actorList);
                               else if(line.indexOf("\t") == 0)
                                    par1 = line.indexOf(")");
                                    par2 = line.indexOf("\t");
                                    trimMovie = (line.substring(par2, par1 +1)).trim();
                                    trimMovie = trimMovie.intern();                              
                                    String ye = trimMovie;
                                    par1 = trimMovie.indexOf("(");
                                    par2 = trimMovie .indexOf(")");                              
                                    ye = (ye.substring(par1 + 1 , par2));                             
                                     addingmovie = (line.trim());
                                           if(addingmovie.indexOf("(TV)") > 0)
                                           else if ( (((Comparable)ye).compareTo("2002")) >= 0)
                                           else  if(addingmovie.indexOf("\"") == 0)
                                           else if(addingmovie.indexOf("(archive footage)") > 0)
                                            else
                                                     if(map1.containsKey(key))
                                                                 ((ArrayList)map1.get(key)).add(trimMovie);                         
                                                              ((ArrayList)map1.get(key)).trimToSize() ;
                                                    else
                                                          movies = new ArrayList();
                                                          movies.add(trimMovie);
                                                          movies.trimToSize() ;
                                                          map1.put(key, movies);
                                              if(map2.containsKey(trimMovie))
                                                 {     ((ArrayList)map2.get(trimMovie)).add(key);
                                                     ((ArrayList)map2.get(trimMovie)).trimToSize() ;
                                            else
                                                           actorList = new ArrayList();
                                                         actorList.add(key);
                                                        actorList.trimToSize() ;
                                                        map2.put(trimMovie, actorList);
    *Compute the amount of shared movies for all actor compared to the one
    *given from the user
    *@param String actor is the actor that the user wish to search for some
    *other actors/actresses with the most shared movies with him
        public void computeSharedMovies( String actor )
             Actor actor2 = new Actor();
             actor2.name = actor;
            if(map1.containsKey(actor2))
                  okactor = 0 ;
                  Actor actor3 = new Actor();
                  actor3 = actor2;
                      for(int count = 0 ; count < ((ArrayList)(map1.get(actor2))).size() ; count++)
                           String movie = (String)((ArrayList)(map1.get(actor2))).get(count);      
                                for (int count2 = 0 ; count2 < ((ArrayList)map2.get(movie)).size() ; count2++)     
                                          Actor iuu = (Actor)((ArrayList)map2.get(movie)).get(count2);
                                          if(!(iuu.name).equals( actor3.name))
                                                     iuu.data++;
             Set entries = map1.entrySet();
             Iterator itr = entries.iterator();
                 List x2 = new ArrayList() ;
                 Actor big = new Actor();
                 big.data = 0;
                 List list = new ArrayList();
             while (itr.hasNext())
                       Map.Entry thisPair = (Map.Entry) itr.next();                 
                          Actor actorCompare = ((Actor)thisPair.getKey());                                   
                          if( actorCompare.data > big.data)
                               big.name = actorCompare.name;
                               big.data = actorCompare.data;
                               list = new ArrayList();
                               list.add(actorCompare);
                      else if (actorCompare.data == big.data)
                                list.add(actorCompare);
          }//end of if, if actor is in map
           else
                   okactor = 1;
    *Prints the final output
    *@param String actor1 is the actor pick by the user to be search
    *@param List most is the list with all the actors/actresses that had
    *the most shared movies
      public void print (String actor1 , List most)
         if(okactor == 0)
               Actor actorPrint = new Actor();
               actorPrint.name = actor1;
               Actor y = new Actor();
               y = actorPrint;
               List  list = new ArrayList();
               list = most;
               int data = ((Actor)list.get(0)).data;
               ArrayList list2 = new ArrayList();
               list2 = (ArrayList)(map1.get(actorPrint));
                  for(int getActor = 0 ; getActor < list.size() ;getActor++)
                           System.out.println(list.get(getActor) + "  :   (" +  data  + "  " + "Shared roles)");
                            Actor name3  = (Actor)list.get(getActor);
                           String na  = name3.name;
                             Map map3 = new HashMap();
                            ArrayList ji = new ArrayList ();
                            ji = (ArrayList)map1.get(list.get(getActor));
                    for (int array1 = 0 ; array1 < ji.size()  ; array1++)
                         map3.put( ji.get(array1) , na);
                    for(int count = 0 ; count < list2.size() ; count++)
                           if(map3.containsKey(list2.get(count)))
                           System.out.println("                    " + (list2.get(count))); 
             Set entries =map1.entrySet();
             Iterator itr = entries.iterator();
             Actor  actortoclean = new Actor();
                  while(itr.hasNext())
                       Map.Entry thisPair = (Map.Entry)itr.next();
                       actortoclean = (Actor)thisPair.getKey();     
                       actortoclean.data = 0 ;      
       else  //else if okactor greater than 0
       System.out.println("THE ACTOR IS NOT IN FILE PLEASE TRY AGAIN") ;
    * Coputes what actors or actresses have the most shared movies
    *return a List with all the actor that have the most shared
    *movies
      public List mostSharedMovies( )
        if(okactor == 0)
        Set entries = map1.entrySet();
        Iterator itr = entries.iterator();
        List x = new ArrayList() ;
        Actor big = new Actor();
        big.data = 0;
        List list = new ArrayList();
          while (itr.hasNext())
                  Map.Entry thisPair = (Map.Entry) itr.next();
                     Actor o1 = ((Actor)thisPair.getKey());
                          if( o1.data > big.data)
                               big.name = o1.name;
                               big.data = o1.data;                 
                               list = new ArrayList();
                               list.add(o1);
                           else if (o1.data == big.data)
                                list.add(o1);
        return list;
      else
         return null;
    *Gives the amount of actor in the map of actors
    *return an int with the quantity
      public int getActorCount( )
          return map1.size();
    *Gives the amount of movies in the map of movies
    *return an int with the quantity
      public int getMovieCount()
           return map2.size();
    }Kevin Bacon Game
    For a description of the Kevin Bacon game, follow this link http://www.cs.virginia.edu/oracle/ . Try the game a few times and see if you can find someone with a Bacon Number higher than 3. In this program you will find all persons with a Bacon Number of 8 or higher. One of these persons is a former President of the United States.
    Strategy
    This is basically a shortest path problem. After that is done, find the large Bacon numbers by scanning the bacon numbers and print out the high-numbered actors and their chains. To print out a high-numbered actor, you should use recursion. Specifically, if some actor x has a Bacon number of b, then you know that they must have been in a movie with someone, call them actor y with a Bacon number of b-1. To print out x's chain, you would print out y's chain (recursively) and then the movie that x and y had in common.
    The Input Files
    There are two data files; both have identical formats. These files are: actors file and actresses file. These files are both compressed in .gz format, and were obtained from the Internet Movie Database. Combined, they are 52 Mbytes (compressed!) and were last updated October 17, 2002. These files are available at ftp://ftp.imdb.com/pub/interfaces/
    These datafiles contain approximately 571,000 actors/actresses in a total of 192,000 movies, with 2,144,000 roles. These files also list TV roles, but you must not include TV roles in your analysis.
    Before you run on the large data sets, use the small (uncompressed) file sample.list(http://www.fiu.edu/~lmore004/cop3530/sample.list) to debug the basic algorithms. In this data file, there are six actors, named a, b, c, d, e, and f, who have been in movies such as X, Y, and Z.
    Input File Hints
    Since it is not my input file, I cannot answer questions about it. Here are some observations that I used in my program, that should suffice. You can read the input file line by line by wrapping a FileInputStream inside a BufferedInputStream inside a GZIPInputStream inside an InputStreamReader inside a BufferedReader. You may not uncompress the file outside of your program.
    There are over 200 lines of preamble that can be skipped. This varies from file to file. However, you can figure it out by skipping all lines until the first occurrence of a line that begins with "Name", and then skipping one more.
    There are many postamble lines, too, starting with a line that has at least nine dashes (i.e. ---------).
    A name is listed once; all roles are together; the name starts in the first column.
    A movie title follows the optional name and a few tab stops ('\t'). There are some messed up entries that have spaces in addition to tab stops.
    The year should be part of the movie title.
    Movies made in 2003 or later should be skipped.
    A TV movie, indicated by (TV) following the year, is to be skipped.
    Archive material, indicated by (archive footage), is to be skipped. (Otherwise JFK is a movie star).
    Cameo appearances, indicated by [Cameo appearance], should be skipped.
    A TV series, indicated by a leading " in the title is to be skipped.
    A video-only movie, indicated by (V) following the year is allowed.
    Blank lines separate actors/actresses, and should be skipped.
    Strategy
    In order to compute your answers, you will need to store the data that you read. The main data structures are a Map in which each key is an Actor and each value is the corresponding list of movies that the actor has been in, and then a second Map, in which key is a movie and each value is the list of Actors in the movie (i.e. the cast). A movie is represented simply as a String that includes the year in which it was made, but an Actor includes both the name of the actor, and a data field that you can use to store computed information later on. Thus, ideally, you would like to define a class that looks somewhat like this (with routines to compute Bacon Numbers not listed):
    public class Database
      private static final class Actor
         String name;
         int    data;  // Bacon number ,
                       // determined by computeBaconNumbers
         public String toString( )
           { return name; }
         public int hashCode( )
           { return name.hashCode( ); }
         public boolean equals( Object other )
           { return (other instanceof Actor) &&
                    ( (Actor) other ).name.equals( name ); }
        // Open fileName; update the maps
      public void loadFile( String fileName ) throws IOException
      public int getActorCount( )
      public int getMovieCount( )
      private Map actorsToMovies = new HashMap( );
      private Map moviesToActors = new HashMap( );
      Memory Details
    The description above is pretty much what you have to do, except that you must take extra steps to avoid running out of memory.
    First, you will need to increase the maximum size of the Java Virtual Machine from the default of 64Meg to 224Meg. You may not increase it any higher than that. If you are running the java interpreter from the command line, the magic option is -Xmx224m. If you are using an IDE, you will have to consult their documentation --- don't ask me.
    Second, you will quickly run out of memory, because if you find two movies that are the same, but are on different input lines, the default setup will create two separate (yet equal) String objects and place them in the value lists of two different actors. Since there are 2.1 million roles, but only 192,000 movies, this means that you will have ten times as many String objects as you really need. What you need to do is to make sure that each movie title is represented by a single String object, and that the maps simply store references to that single String object. There are two basic alternatives:
    The String class has a method call intern. If you invoke it, the return value on equal Strings will always reference the same internal String object.
    You can keep a HashMap in which each key is a movie title, and each value is the same as the key. When you need a movie title, you use the value in the HashMap.
    Option two is superior (performancewise) to option #1 and it is required that you use it to avoid memory problems.
    When you maintain the list of movies for each actor, you will want to use an ArrayList. It takes little effort to ensure that the capacity in the ArrayList is not more than is needed, and you should do so, to avoid wasting space (since there are 571,000 such array lists).
    When you construct the HashMaps, you can issue a parameter (the load factor). The higher the load factor, the less space you use (at the expense of a small increase in time). You should play around with that too; the default is 0.75; you will probably want a load factor of 2.00 or 3.00.
    You must be very careful to avoid doing any more work in the inner loops than you need to, including creating excessive objects. IF YOU CREATE EXCESSIVE OBJECTS, YOUR PROGRAM MAY SLOW TO A CRAWL BECAUSE ALL ITS TIME WILL BE SPENT IN THE GARBAGE COLLECTOR OR RUN OUT OF MEMORY COMPLETELY.
    What to Submit
    Submit complete source code and the actors/actresses with Bacon Numbers of 8 or higher. Include the complete paths for each of the actors/actresses (with shared movie titles). Also indicate how long your algorithm takes. This means how long it takes to load, and also how long it takes to run the shortest path computation (not including the output of the answer), by inserting calls to System.currentTimeMillis at appropriate points in your code, and tell me how many actors and movies there are. Also, insert this code (at the end of your program) that tells me how large the VM is:
    Runtime rt = Runtime.getRuntime( );
    int vmsize = (int) rt.totalMemory( );
    System.out.println( "Virtual machine size: " + vmsize );
    Don't forget to write in the processor speed of the computer you are using. If it's somewhat fast, or provably space-efficient, you can get extra credit. You cannot receive credit for a working program if your program fails to produce a large set of actors and actresses with Bacon Numbers of 8 or higher. Note: the data you will work on and the data the Oracle uses (and the IMDB data) are all slightly out of sync. So you might not be able to exactly reproduce the results, although I was able to get a complete set of Bacon Numbers 8 and higher when I ran my program on October 23, using the Oct 17 files.
    Due Date
    This program is due on Thursday November 14.
    Additional Notes
    The exact sizes of the data files are 36381624 and 18263563 bytes, respectively. If you download and the files are larger, then you messed up the download. Note that if you are using Windows XP, these files might be uncompressed during downloading. If so, you can use this program to recompress the file. If the files are smaller, then the download probably got interrupted before it finished and you will need to retry. Here is a gzipped sample.list for you to test that aspect of your program.
    Running on COTTON in the AUL, which is a 450 MHz Pentium III, and accessing the files over the network as "\\\\couger\\cop3530\\actors.list.gz", (and similarly for actresses.list.gz), and with no other processes running besides notepad, the data files loaded in 180 seconds. You should be able to get faster results on a faster machine. Some of the AUL machines are 1.6 GHz. You should indicate which AUL machine (with processor speed) ran your submission.
    Entries with years such as (1996/I) are optional.
    When writing and reading make sure you are using BufferedInputStream and BufferedOutputStream, as appropriate.
    Sketch of the basic shortest path computation:
    // typically invoked with Kevin Bacon as parameter
    // This is the basic algorithm; there's stuff

    I forgot to post the code, here it is:
    import java.util.LinkedList ;
    import java.io.*;
    import java.util.zip.*;
    import java.util.ArrayList;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Set;
    import java.util.Iterator;
    import java.util.HashSet;
    * A class that download files , create Maps and search for information with
    public class DataBase2 implements Serializable
      private Map map1 = new HashMap((int)515927, (float)0.85 );  // actor
      private Map map2= new HashMap((int)172911, (float)0.85 );   //movies
      transient private Map map3 = new HashMap();   //movie and movie
      transient private int okactor = 0;
      transient static final int INFINITY = Integer.MAX_VALUE;
    // Main Method
      public static void main (String args [])throws IOException
        int c  = 0 ;
        System.out.println("Program was run in Pentium 4 in AUL");        
        DataBase2 testing = new DataBase2();   
        try{   
            ObjectInputStream ii1 = new ObjectInputStream (new BufferedInputStream (new FileInputStream("file2.ser")));
        catch (FileNotFoundException ex)
            System.out.println("FILE NOT ON DISK PLEASE WAIT WHILE DOWNLOADING");
            long x = System.currentTimeMillis();
            testing.loadFile("\\\\Cougar\\cop3530\\actresses.list.gz");   
            testing.loadFile("\\\\Cougar\\cop3530\\actors.list.gz");
            long y = System.currentTimeMillis();
            System.out.println("DONE WITH DOWNLOADING, USING NORMAL METHOD " +(y - x)/1000 + " seconds");                       
            System.out.println("The actors count is:  " + testing.getActorCount());
            System.out.println("The movies count is:  "  + testing.getMovieCount());
            testing.loadser(testing);      
            System.out.println("END OF DUMPING");      
            testing.findBacon();  
            c++;        
        if (c == 0 )
            long o= System.currentTimeMillis();   
            testing = testing.fastloading(testing);
            long y = System.currentTimeMillis(); 
            System.out.println("DONE WITH LOADING, FROM SERIALIZED FILES  " + (y-o)/1000 + "seconds");         
            System.out.println("The actors count is:   " + testing.getActorCount());
            System.out.println("The movies count is:   " + testing.getMovieCount());
            testing.findBacon();             
            Runtime rt = Runtime.getRuntime( );       
            int vmsize = (int) rt.totalMemory( );       
            System.out.println(" " );
            System.out.println( "The Virtual Machine size is : " + vmsize + " bytes" );
    //static class actor 
      private static final class Actor implements Serializable
         String name;
         int    data;  // number of shared movies,
                       // determined by computeSharedMovies          
         public String toString( )
           { return name; }
         public int hashCode( )
           { return name.hashCode( ); }         
         public boolean equals( Object other )
           { return (other instanceof Actor) && ( (Actor) other ).name.equals( name );}
    *Method to find the bacon number of each actor
    *and also send information, actors greater than 7,  to be print
      public void findBacon()
        ArrayList actorlist = new ArrayList();
        long time1 = System.currentTimeMillis();
        Actor actor2 = new Actor();    
        actor2.name = "Bacon, Kevin";
        actor2.data = 0;
        Set entries =map1.entrySet();
        Iterator itr = entries.iterator();
        Actor  actortoInfinity = new Actor();
        Set listofMovies = new HashSet();          
                    while(itr.hasNext())
                            Map.Entry thisPair = (Map.Entry)itr.next();
                            actortoInfinity = (Actor)thisPair.getKey();    
                            if((actortoInfinity.name).equals( actor2.name))
                                    actortoInfinity.data = 0 ;                     
                            else
                            actortoInfinity.data = INFINITY ;      
         Actor actor3 = new Actor() ;      
         LinkedList list = new LinkedList();
         list.addLast(actor2) ;
         Actor out = new Actor();
                    while (list.isEmpty() != true)
                            out = (Actor)list.getFirst();
                        list.removeFirst() ;
                                    for(int count = 0 ; count < ((ArrayList)(map1.get(out))).size() ; count++)
                                            String movie = (String)((ArrayList)(map1.get(out))).get(count);                      
                            if(listofMovies.contains(movie))
                            {continue;}
                            else
                            listofMovies.add(movie);
                                    for(int count2 = 0; count2 < ((ArrayList)(map2.get(movie))).size() ; count2++)
                                                    actor3  =   (Actor)(((ArrayList)(map2.get(movie))).get(count2) )  ;
                                            if(actor3.data == INFINITY)
                                                actor3.data = out.data + 1;
                                                list.addLast(actor3) ;
                                    if(actor3.data >= 7)
                                            actorlist.add(actor3);                                                                 
                                                             }//inner loop                
                         }//else
                     }//outer loop
                 }//while loop
         long time2 = System.currentTimeMillis();
         System.out.println("Done gettig Bacon Number(shortest path): " + (time2 - time1)/1000 + " Seconds");
                    for(int count = 0 ; count < actorlist.size() ; count++)
                                    print((Actor)(actorlist.get(count)));
    *Method used to print the chain of information within
    *each actor with Bacon number of 7 and over
    *it uses recursion to do it
      private void   printPath (Actor target)
        int c = 0 ;  
        Actor actor = new Actor();
            String movies ;
              if (target.data == 0)
                  System.out.println("-----------------------------END OF PATH-----------------------------");                           
              else
                    for(int x = 0 ; x < ((ArrayList)(map1.get(target))).size(); x++)
                    movies = (String)(((ArrayList)(map1.get(target))).get(x));
                    for (int y = 0 ; y < ((ArrayList)map2.get(movies)).size() ;  y++)
                            actor = (Actor)(((ArrayList)(map2.get(movies))).get(y));
                            if(actor.data == target.data - 1)
                                    System.out.println("* " + target + "  acted with " + actor+ " how's BK # is " + actor.data + ", both in movie: " + movies ) ;
                                c++;
                                break;                      
                     }//inner loop
                   if (c > 0)
                   break;
                  }//outer loop
              printPath(actor);
    *Gets the actor to print
    *and sends the same actor to
    *PrintInfo method to print
    *its chain
    *@param target the actor to print
      public void print(Actor target)
                            System.out.println(" " );
                                    System.out.println(" " ); 
                            System.out.println(" " );
                                    System.out.println(target.name + "'s Bacon number is: " +target.data);
                                    System.out.println(" " );
                                printPath(target);                                                                                                                                                                         
    *A method to load the Serialization file
    *so it,the file,  can be manipulated
    *@param x the DataBase object to be load
    *with all the files
    *@return the object x with all the file
    *withit
      public DataBase2 fastloading(DataBase2  target2) throws IOException
            try{ 
                    ObjectInputStream ii1 = new ObjectInputStream (new BufferedInputStream (new FileInputStream("file2.ser")));
                    target2= (DataBase2) ii1.readObject();
                    ii1.close();
        catch(ClassNotFoundException e )
           System.out.println("Error, class exception");
        catch(FileNotFoundException ex)
           System.out.println("Error, the file is not on the disk");       
        return target2;            
    *Method to write the Object DataBase2, created before, into a Serializable file
      public void loadser(DataBase2 ObjecttoLoad ) throws IOException
            ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("file2.ser")));
            oo.writeObject(ObjecttoLoad);
            oo.close();
    /**Method use to download files for the first time
    *it creates maps, which contains actor and movies
    *@param fileName it gets the file to be read and
    *used to get the data into the maps
      public void loadFile( String fileName ) throws IOException
          BufferedReader x = new BufferedReader ( new InputStreamReader(new GZIPInputStream (new BufferedInputStream( new FileInputStream(fileName)))));
          String line;          
          int start = 0 ;    
          ArrayList actorList = new ArrayList();
          ArrayList movies = new ArrayList();
          Actor key = new Actor();
          int p = 0;    //parameters
          int p2 =0 ;
          String you = null ;
          String year = null;
          String trimMovie = null;
          int par1 = 0;    //parameter
          int par2 =0;
          String addingmovie = null;
            while((line = x.readLine()) != null)
                if(line.indexOf("Name") == 0 )
                    start++;                       
                if(start == 0)
                continue;                       
                if( start >= 1)
                                  if(line.indexOf("-----------------------") == 0)
                                         break;                    
                                  if(((line.trim()).length()) == 0)
                                     continue;
                                  else if(line.indexOf("----") == 0)
                                     continue;
                                      else if (line.indexOf("Name") == 0)
                                             continue;
                                  else if(line.indexOf("\t") != 0)
                                     p  = line.indexOf("\t");
                                     p2 = line.lastIndexOf(")");                               
                                     String actor = (line.substring(0,p));                                        
                                             key = new Actor();
                                             key.name = actor;                               
                                             you = (line.substring(p, (p2 + 1)));                                                                                                                                                                                                                                                                              
                                             if (you.indexOf("(TV)") > 0)
                                                            continue;                                                                                              
                                     p = you.indexOf("\t");
                                     p2 = you.indexOf(")");
                                         you = (you.substring(p, p2 +1)).trim();                                    
                                          if(you.indexOf("\"") == 0)
                                                             continue;                      
                                     year = you ;      
                                     p = year.indexOf("(");
                                     p2 = year.indexOf(")");
                                     year = year.substring(p + 1 , p2);                                 
                                           if ( ( ((Comparable)year).compareTo("2002") ) >= 0)
                                                  continue;                                           
                                           if (map3.containsKey(you))
                                               else
                                                          map3.put(you, you);                                                                                              
                                     movies = new ArrayList();
                                     movies.add(map3.get(you));
                                     movies.trimToSize() ;                         
                                     map1.put(key , movies);
                                          if(map2.containsKey(map3.get(you)))
                                                              ((ArrayList)map2.get(map3.get(you))).add(key) ;
                                          else
                                                              actorList = new ArrayList();
                                                                          actorList.add(key);
                                                                      actorList.trimToSize() ;                                                                     
                                                                      map2.put(map3.get(you), actorList);
                             else if(line.indexOf("\t") == 0)
                                    par1 = line.indexOf(")");
                                    par2 = line.indexOf("\t");                                                          
                                    trimMovie = (line.substring(par2, par1 +1)).trim();
                                    // trimMovie = trimMovie.intern();                           
                                    if(map3.containsKey(trimMovie))
                                    else
                                    map3.put(trimMovie , trimMovie);
                                    String ye = (String)map3.get(trimMovie);
                                    par1 = trimMovie.indexOf("(");
                                    par2 = trimMovie .indexOf(")");                             
                                    ye = (ye.substring(par1 + 1 , par2));                                                               
                                    addingmovie = (line.trim());
                                        if(addingmovie.indexOf("(TV)") > 0)
                                            else if ( (((Comparable)ye).compareTo("2002")) >= 0)
                                            else  if(addingmovie.indexOf("\"") == 0)
                                            else if(addingmovie.indexOf("(archive footage)") > 0)
                                        else
                                            if(map1.containsKey(key))
                                                            ((ArrayList)map1.get(key)).add(map3.get(trimMovie));                            
                                                            ((ArrayList)map1.get(key)).trimToSize() ;
                                            else
                                                    movies = new ArrayList();
                                                    movies.add(map3.get(trimMovie));
                                                    movies.trimToSize() ;
                                                    map1.put(key, movies);
                                           if(map2.containsKey(trimMovie))
                                                {   ((ArrayList)map2.get(map3.get(trimMovie))).add(key);
                                                    ((ArrayList)map2.get(map3.get(trimMovie))).trimToSize() ;
                                               else
                                                            actorList = new ArrayList();
                                                                    actorList.add(key);
                                                            actorList.trimToSize() ;
                                                            map2.put(map3.get(trimMovie), actorList);
                                      }     //end of last else if          
                        }//end of if
                }//end of while
        }//end of method
    *Gives the amount of actor in the map of actors
    *return an int with the quantity
      public int getActorCount( )    
          return map1.size();
    *Gives the amount of movies in the map of movies
    *return an int with the quantity
      public int getMovieCount()
           return map2.size();
    }//end of DataBase2 class

  • Loading a text file in a gzip or zip archive using an applet to a String

    How do I load a text file in a gzip or zip archive using an applet to a String, not a byte array? Give me both gzip and zip examples.

    This doesn't work:
              try
                   java.net.URL url = new java.net.URL(getCodeBase() + filename);
                   inputStream = new java.io.BufferedInputStream(url.openStream());
                   if (filename.toLowerCase().endsWith(".txt.gz"))
                        inputStream = (java.io.InputStream)(new java.util.zip.GZIPInputStream(inputStream));
                   else if (filename.toLowerCase().endsWith(".zip"))
                        java.util.zip.ZipInputStream zipInputStream = new java.util.zip.ZipInputStream(inputStream);
                        java.util.zip.ZipEntry zipEntry = zipInputStream.getNextEntry();
                        while (zipEntry != null && (zipEntry.isDirectory() || !zipEntry.getName().toLowerCase().endsWith(".txt")))
                             zipEntry = zipInputStream.getNextEntry();
                        if (zipEntry == null)
                             zipInputStream.close();
                             inputStream.close();
                             return "";
                        inputStream = zipInputStream;
                   else
                   byte bytes[] = new byte[10000000];
                   byte s;
                   int i = 0;
                   while (((s = inputStream.read())) != null)
                        bytes[i++] = s;
                   inputStream.close();
            catch (Exception e)
              }

  • Error while installing Sneak preview Netweaver 2004 --SP15 on WindowsXP+SP1

    hi, i'm getting a error while installing the Sneak preview NetWeaver 2004-SP15 on Windows XP + SP1.
    error message: Corrupt GZip trailer
    JDK version: 1.4.2_08 and 1.4.2_09
    i searched this error on google and i found it's a bug of jdk1.4.2_08.(see this page:<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262583">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262583</a>) it said this version can't run when the uncompressed file is 2GB or larger. and this bug was fixed after <b>jdk1.5</b>. but i noted that Sneak preview was not tested on jdk1.5.Ignore it, i uninstalled jdk1.4.2_08 and Sneak preview,then i installed jdk1.5 and reinstalled Sneak preview.Unfortunated, i got another error.The error code is <b>3018</b>.
    could anyone please help me?
    Regards
    bo

    I faced this problem while I was installation.
    Following are my installation's step, is there any problem?
    1.I have a new system just for installation, it is WindowsXP + SP1(2.5G memory and 2 CPU);
    2.download JDk 1.4.2_09 from sun.com and installed it;
    3.download Sneak preview Netweaver2004SP15 from sdn.sap.com. There are 3 files total.
      Two are NW04SneakPreviewSP15.rar, the other is DeveloperStudioSP15.rar;
    4.uncompressed NW04SneakPreviewSP15.rar. I used winrar, it's OK.
    5.start install NW04SneakPreviewSP15.rar step by step according to the installation guid.
    6.to this step 'SAP J2EE Engine Server Core', all are right;
    7.error in the step 'Load Java Database Content';
    Following are part of the log file 'jload.java.log':
    java version "1.4.2_09"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_09-b05)
    Java HotSpot(TM) Client VM (build 1.4.2_09-b05, mixed mode)
    2006-3-22 18:26:37 com.sap.inst.jload.Jload main
    ÐÅÏ¢: Jload -sec J2E,jdbc/pool/J2E,C:usrsapJ2ESYSglobal/security/data/SecStore.properties,C:usrsapJ2ESYSglobal/security/data/SecStore.key -dataDir C:/backup/NW04SneakPreviewSP15/NWSneakPreviewSP15/SAP_NetWeaver_04_SR_1_Installation_MasterIM01_NT_I386....SneakPreviewContentJDMP -job C:Program Filessapinst_instdirNW04SR1WEBAS_COPYONE_HOST/IMPORT.XML -log C:Program Filessapinst_instdirNW04SR1WEBAS_COPYONE_HOST/jload.log
    2006-3-22 18:26:40 com.sap.inst.jload.db.DBConnection connectViaSecureStore
    ÐÅÏ¢: connected to J2E on jdbc/pool/J2E
    2006-3-22 18:26:41 com.sap.inst.jload.JobStatus readStatus
    ÐÅÏ¢: trying to read status file C:usrsapJ2EJC00j2eesltoolsIMPORT.sta
    2006-3-22 18:26:41 com.sap.inst.jload.JobStatus readStatus
    ÐÅÏ¢: status file IMPORT.sta doesn't exist - no restart
    2006-3-22 18:26:41 com.sap.inst.jload.Jload dbImport
    ÐÅÏ¢: trying to create table ADS_LICENSED_FORMS
    2006-3-22 18:26:42 com.sap.inst.jload.Jload dbImport
    ÐÅÏ¢: table ADS_LICENSED_FORMS created
    2006-3-22 18:26:42 com.sap.inst.jload.Jload dbImport
    ÐÅÏ¢: ADS_LICENSED_FORMS loaded (2 rows)
    2006-3-22 18:48:58 com.sap.inst.jload.Jload dbImport
    ÐÅÏ¢: trying to create table J2EE_CONFIGENTRY
    2006-3-22 18:48:58 com.sap.inst.jload.Jload dbImport
    ÐÅÏ¢: table J2EE_CONFIGENTRY created
    2006-3-22 19:07:53 com.sap.inst.jload.Jload logStackTrace
    ÑÏÖØ: java.io.IOException: Corrupt GZIP trailer
    at java.util.zip.GZIPInputStream.readTrailer(GZIPInputStream.java:174)
    at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:89)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:105)
    at java.io.DataInputStream.readBoolean(DataInputStream.java:310)
    at com.sap.inst.jload.db.FieldHandler.checkAndSetNullValue(FieldHandler.java:46)
    at com.sap.inst.jload.db.BigIntHandler.setValue(TinyIntHandler.java:164)
    at com.sap.inst.jload.db.DBTable.load(DBTable.java:274)
    at com.sap.inst.jload.Jload.dbImport(Jload.java:323)
    at com.sap.inst.jload.Jload.executeJob(Jload.java:397)
    at com.sap.inst.jload.Jload.main(Jload.java:621)
    2006-3-22 19:07:53 com.sap.inst.jload.db.DBConnection disconnect
    ÐÅÏ¢: disconnected
    I also tried jdk1.4.2_08 and I also installed in Windows2000+SP4. But I faced the same problem too.
    From sun.com, I know this error is a bug of jdk. They explained that this version can't run OK
    when zip-file is larger than 2G. Then I found here's a file
    (name is 'NW04SneakPreviewSP15NWSneakPreviewSP15SneakPreviewContentSDMSDMKIT.JAR'). It's size
    is 2.08G. I suppose installation can't manage it.
    I tried unzip this file and renamed unziped folder to 'SDMKIT.JAR'.
    Then I found another large file in the unziped folder(name is 'F:NW04SneakPreviewSP15NWSneakPreviewSP15SneakPreviewContentSDMSDMKIT.JARpackpackedRoot.jar')
    , so I also unzip this file and renamed unziped folder to 'packedRoot.jar'.
    Then I reinstalled, of course, It's no work.
    Unexpectedly, I found this download link is not available on the sdn.sap.com(see this page:https://www.sdn.sap.com/irj/sdn/downloaditem?rid=/library/uuid/cfc19866-0401-0010-35b2-dc8158247fb6).
    It say that 'We apologize that this version is currently not available to download.'.
    I guess there are any problem in this version, are there?
    So, now I think I must download another version to install.
    Thanks,
    Bo
    Message was edited by: bo zeng

Maybe you are looking for

  • ISE Using my device Portal , devices still in pending registration status

    Abstract: I'm on ISE 1.2 patch 8. We want give access wireless to devices mobile using 802.1x with Active Directory. The condition is that he previously the user must register mobile device in "my device portal" -The corporate user connected from the

  • Line wrap in JSE 8

    Hi, Is there a way to wrap around long line on the editor? I don't want a hard wrap, just a soft wrap around the window. Couldn't find any such option. Thanks.

  • Turn off mavericks upgrade notification in Snow Leopard

    I'm happily running Snow Leopard with no plans to upgrade, but lately I've been getting uninvited update to mavericks notifications front and center on my screen in the middle of whatever I happen to be doing and Apple doesn't appear to offer any way

  • Text to Speech not working on Mac

    I downloaded a PDF file because it was not avaliable in ebook form. When I activated text to speech there was no output.

  • Fetch the number of users in bulk request

    Hi All, We are working on OIM 11gR1. While raising request "Modify User", when we are submitting the request with multiple users, how can we fetch the number of users selected to be modified in java code? Regards, Aparna