1 2 Previous Next 19 Replies Latest reply on May 6, 2009 6:41 AM by EJP

    InputStream not returning end of file

    843789
      I am having a problem regarding inputstream and the -1 that it is supposed to return. I have a server that returns an audio file when an english text is given. The server that I am using is the Festival server created by University of Edinburgh. The client that I wrote provides the server with an english text and in return it is giving me a bunch of bytes but after exactly 44 iterations in the while loop the client stops working. I mean the client program does not actually crash but after 44 iterations, the client actually sits for the next byte to arrive. The problem is that the server is not returning -1 which denotes end of file.

      What should I be doing? How can I make the server return a negative 1? If it does not return a -1 then is there a way I can manually insert the -1 to force shutdown the program?
        • 1. Re: InputStream not returning end of file
          EJP
          The problem is that the server is not returning -1 which denotes end of file.
          The problem is that the server isn't closing the output stream. So it isn't the end of the stream.

          NB an input stream from a socket isn't a file.
          • 2. Re: InputStream not returning end of file
            843789
            So, what should I be doing in a case like this?
            • 3. Re: InputStream not returning end of file
              843789
              Oh and one more thing. If i know the last line of the file or the last byte being sent, then can I close the outputstream and insert -1 in the byte array?
              • 4. Re: InputStream not returning end of file
                843789
                In your place I would first return the length of the file and then the file content. That way you know how many bytes to read and you don't rely on EOF. DataOutputStream and DataInputStream are very useful in this context.
                • 5. Re: InputStream not returning end of file
                  843789
                  Thnks for the reply but how would I know the length of the file. The server is basically sending me raw bytes of data which i need to read and then write to a file from the client side. The server is doing nothing but give me streaming bytes of data. How would I know that the server has stopped returning and that it has closed the outputstream from its side?
                  • 6. Re: InputStream not returning end of file
                    843789
                    InSaNe wrote:
                    Thnks for the reply but how would I know the length of the file. The server is basically sending me raw bytes of data which i need to read and then write to a file from the client side. The server is doing nothing but give me streaming bytes of data. How would I know that the server has stopped returning and that it has closed the outputstream from its side?
                    In your OP you said "How can I make the server return a negative 1" which I take to mean that you have control over the server code and can modify it at will. So, modify it so that it first sends the file length.
                    • 7. Re: InputStream not returning end of file
                      EJP
                      ... and don't use -1 as an EOF marker. It can occur within files.
                      • 8. Re: InputStream not returning end of file
                        843789
                        As I have mentioned in my first post that the server is the Text-to-Speech server created by the University of Edinburgh. I do not have control over the server side. The server is sending me raw bytes and nothing more. I need to read those and write them to a file. If it helps, I am writing to write to an audio file.
                        • 9. Re: InputStream not returning end of file
                          EJP
                          Then either the server is implementing an unusable protocol, or failing to close the connection when it should, or it has already sent you the length of the data and you are ignoring it, or you have a coding error.
                          • 10. Re: InputStream not returning end of file
                            843789
                            InSaNe wrote:
                            As I have mentioned in my first post that the server is the Text-to-Speech server created by the University of Edinburgh. I do not have control over the server side. The server is sending me raw bytes and nothing more. I need to read those and write them to a file. If it helps, I am writing to write to an audio file.
                            Then you are stuffed. How long is a piece of string? Unless the Text-to-Speech server sends some indication as to when it has completed (length or EOF) a 'file' transfer then you have no way of knowing when the 'file' transfer is complete.
                            • 11. Re: InputStream not returning end of file
                              843789
                              The following is the code that I am writing. Let me know if I making any mistake.
                              public class Main{
                                public static void main (String [] args ) throws IOException {
                                  int filesize=102400; 
                                  String message = "Hello World world asas asas asas asas asas";
                              
                                  Socket sock = new Socket("192.168.254.128",1314);
                                  System.out.println("Connecting...");
                                  
                                  OutputStream os = sock.getOutputStream();
                                  os.write("(Parameter.set 'Wavefiletype 'riff)\n".getBytes());
                                   os.write(("(tts_text \""+message+"\" 'file)\n").getBytes());
                              
                                  byte [] input  = new byte [filesize];
                                  InputStream is = sock.getInputStream();  
                                  
                                  FileOutputStream fos = new FileOutputStream("C:\\Documents and Settings\\Desktop\\file.wav");
                                  //BufferedOutputStream bos = new BufferedOutputStream(fos);
                               
                                   System.out.println("I am outside the for loop and iput length is: "+input.length);
                                   int i=0;
                                   for(i=0;i<input.length;i++) {
                                        int b = is.read();
                                        input=(byte)b;     
                                        if(b==-1) {
                                             break;
                                        }
                                   }
                                   

                                   ////////////////////////////////////////////////////////////
                              fos.write(input);
                              fos.close();
                              is.close();
                              sock.close();
                              }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                              • 12. Re: InputStream not returning end of file
                                EJP
                                int filesize=102400;
                                How do you know that the file is 102400 bytes long?
                                     for(i=0;i<input.length;i++) {
                                          int b = is.read();
                                          input=(byte)b;     
                                          if(b==-1) {
                                               break;
                                That assignment and test are obviously in the wrong order, but the whole approach of reading the entire thing into memory is wrong. I'm also wondering whether your test doesn't really read
                                if (input[i] == -1)
                                or possibly 'b' is declared as a byte. In either case comparing a byte to -1 can never be true, by the properties of bytes in Java. One of these could be your error. In any case the loop above should read:
                                int count;
                                while ((count = in.read(input)) > 0)
                                  fos.write(buffer, 0, count);
                                fos.close();
                                is.close();
                                And if this still blocks, complain to the owners of the server.
                                • 13. Re: InputStream not returning end of file
                                  843789
                                  InSaNe wrote:
                                  The following is the code that I am writing. Let me know if I making any mistake.
                                  The obvious mistake is, as you are aware, that you are assuming that end-of-record is signalled by the read returning -1. Obviously is does not.

                                  P.S. There are more efficient way to read the input stream and one does not have to hold the whole file in memory before writing it out.
                                  • 14. Re: InputStream not returning end of file
                                    843789
                                    With what size do i initialize the buffer as this is the byte array?
                                    1 2 Previous Next