14 Replies Latest reply: Aug 3, 2009 11:07 AM by PhHein RSS

    Reading a file behaving differently on Vista?

    807588
      I've been using the following code for a while now to read a file into a byte array:

      FileInputStream stream=new FileInputStream(file);
      byte[] bytes=new byte[(int) file.length()];
      stream.read(bytes);

      This works perfectly well on Windows XP, Mac OS X and Ubuntu. However, it seems that sometimes the operation fails on Windows Vista. I know the common way is to have some array with a fixed length and load the file slowly into that, but when I check the return value for InputStream.read() is identical to the returned value of file.length().

      So, I'm sort of lost why it doesn't work on Vista in all cases. Maybe it has something to do with the weird "virtual folders" system, where applications are not allowed to read from certain system folders but instead read from some "fake" folder. I was wondering if anyone else had encountered this problem. It's very likely that this is my fault somehow, but I'm wondering why it only fails on Vista.
        • 1. Re: Reading a file behaving differently on Vista?
          807588
          You need to read the Javadoc for InputStream. The read() method does not guarantee to read the number of bytes requested and it returns the number of bytes read. You have just been lucky in the past.

          Wrap your InputStream in a DataInputStream and use the method readFully().
          • 2. Re: Reading a file behaving differently on Vista?
            807588
            I know, I know. But after the read I check if the two values are identical:
            FileInputStream stream=new FileInputStream(file);
            byte[] bytes=new byte[(int) file.length()];
            
            if (stream.read(bytes)!=bytes.length) {
                 throw new IOException("Problem while reading file");
            }
            But...this exception is never thrown! So even though the array length is equal to the file size there are still problems when I try to open the file later on.
            • 3. Re: Reading a file behaving differently on Vista?
              807588
              That is just silly. You are turning a perfectly valid result into an IOException when you actually have a coding error. I don't use Vista so I can't say where Vista fits into this but I do know that your code is flawed and must be changed.
              • 4. Re: Reading a file behaving differently on Vista?
                807588
                OK, even though this is only a simple test I'll change it. But even though the test is poor I still don't have a clue of what is different in Vista :(

                Edit: now with better example, although the original problem still applies:
                FileInputStream input=new FileInputStream(file);
                ByteArrayOutputStream output=new ByteArrayOutputStream((int) file.length());
                byte[] buffer=new byte[2048];
                int length=0;
                          
                while ((length=input.read(buffer,0,buffer.length))!=-1) {
                     output.write(buffer,0,length);
                }
                
                input.close();
                          
                byte[] bytes=output.toByteArray();
                Edited by: keeskist on Feb 5, 2008 7:43 AM
                • 5. Re: Reading a file behaving differently on Vista?
                  807588
                  What am I missing? Why are you not saving a whole load of code and using DataInputStream.readFully() ?
                  • 6. Re: Reading a file behaving differently on Vista?
                    802908
                    Hi,

                    i recently changed my main application for Vista. Among other things i made the following change:
                    For my database (and ini files and other stuff) i use an own subdirectory under
                     String AllUsers = System.getenv("ALLUSERSPROFILE");
                    This path is not affected by virtualisation.

                    regards

                    BB
                    • 7. Re: Reading a file behaving differently on Vista?
                      807588
                      Application data is already saved in %APPDATA%, but unfortunately we have no control over where this particular file is located. I'll explain the point to make my question more clear:

                      The (Swing) application requires you to select any image file from the hard drive via a file chooser. The file is then converted to a byte[], and used as a parameter for XFire. However, either XFire has problems with Vista, or I'm doing something wrong. The latter seems to be the more likely.

                      The method looks something like upload(byte[] file), and the resulting stack trace is as follows:

                      java.io.IOException: Unexpected end of ZLIB input stream
                      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

                      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
                      rce)
                      at java.lang.reflect.Constructor.newInstance(Unknown Source)
                      at org.codehaus.xfire.aegis.type.basic.BeanType.createFromFault(BeanType
                      .java:260)
                      at org.codehaus.xfire.aegis.type.basic.BeanType.readObject(BeanType.java
                      :120)
                      (more...)
                      • 8. Re: Reading a file behaving differently on Vista?
                        802908
                        Hmm... looks more like a problem with the input stream.
                        I would try sabres proposal...

                        regards

                        BB
                        • 9. Re: Reading a file behaving differently on Vista?
                          807588
                          Still the same exception with the following code:
                          DataInputStream input=new DataInputStream(new FileInputStream(file));
                          byte[] bytes=new byte[(int) file.length()];
                          input.readFully(bytes);
                          • 10. Re: Reading a file behaving differently on Vista?
                            807588
                            keeskist wrote:
                            Still the same exception with the following code:
                            DataInputStream input=new DataInputStream(new FileInputStream(file));
                            byte[] bytes=new byte[(int) file.length()];
                            input.readFully(bytes);
                            Post the full code and stack trace to continue...
                            • 11. Re: Reading a file behaving differently on Vista?
                              807588
                              The GUI application contains the following method:
                              public void upload(File file) throws IOException {
                                        
                                   DataInputStream input=new DataInputStream(new FileInputStream(file));
                                   byte[] bytes=new byte[(int) file.length()];
                                   input.readFully(bytes);
                                   input.close();
                                        
                                   serverConnection.upload(file.getName(),bytes);
                              The variable serverConnection is an interface for a method that is called on the server through XFire. The method on the server looks as follows:
                              public void upload(String name, byte[] bytes) throws IOException {
                              
                                   File file = new File(dataFolder, name);
                                   OutputStream os = null;
                                        
                                   if (!file.exists() && !file.createNewFile())
                                        throw new NoAccessRightException();
                                             
                                   try {
                                        os = new BufferedOutputStream(new FileOutputStream(file));
                                        os.write(bytes);
                                        os.close();
                                   } catch (IOException e) {
                                        if (os != null) { 
                                             try {
                                                  os.close();
                                             } catch (IOException e1) { }
                                        }
                                        throw e;
                                   }
                              }
                              When this method is called, in some cases (but not all, and only on Windows Vista) the following exception is thrown:

                              java.io.IOException: Unexpected end of ZLIB input stream
                              at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                              at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
                              at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source)
                              at org.codehaus.xfire.aegis.type.basic.BeanType.createFromFault(BeanType.java:260)
                              at org.codehaus.xfire.aegis.type.basic.BeanType.readObject(BeanType.java:120)
                              at org.codehaus.xfire.aegis.AegisBindingProvider.readParameter(AegisBindingProvider.java:160)
                              at org.codehaus.xfire.client.ClientFaultConverter.processFaultDetail(ClientFaultConverter.java:51)
                              at org.codehaus.xfire.client.ClientFaultConverter.invoke(ClientFaultConverter.java:32)
                              at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
                              at org.codehaus.xfire.client.Client.onReceive(Client.java:400)
                              at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)
                              at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
                              at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
                              at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
                              at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:75)
                              at org.codehaus.xfire.client.Client.invoke(Client.java:335)
                              at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
                              at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
                              at $Proxy11.upload(Unknown Source)
                              at com.adjustables.addesigner.util.AdServerConnection.upload(AdSe
                              rverConnection.java:141)
                              • 12. Re: Reading a file behaving differently on Vista?
                                807588
                                Hello keeskist,

                                Did u able to find the reason for this error?

                                Even am facing a similar problem with a java tool with client-server architecture.

                                The upload of a file from client to server fails ONLY in vista environment.

                                But UNIX, Win XP client is able to upload the file without any prob.

                                Some people say that Vista have extra firewall security settings which blocks the file upload.

                                Please help me if you have any info on this.

                                Best Regards,
                                Ashik
                                • 13. Re: Reading a file behaving differently on Vista?
                                  807588
                                  Post your question in a new thread. Obviously the original poster wasn't up to the challenge, why are you seeking help from him?
                                  • 14. Re: Reading a file behaving differently on Vista?
                                    PhHein
                                    Brushfire wrote:
                                    Post your question in a new thread. Obviously the original poster wasn't up to the challenge, why are you seeking help from him?
                                    Yup, I'm lolcking this zombie.