Results 1 to 16 of 16

Thread: Write String to InputStream

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Question Write String to InputStream

    Does anyone know how to write a String into a OutputStream and connect that OutputStream to a InputStream in the same program? Please kindly help. Thanks in advance.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Write String to InputStream

    Output and Input streams are abstract classes, you must instantiate the appropriate subclass depending to the type of data you are reading/writing.

    What do you mean by "Connecting the input and output streams"?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Re: Write String to InputStream

    Code:
     
                PipedOutputStream ps = new PipedOutputStream();
                PipedInputStream is = new PipedInputStream(ps);
                PrintStream os = new PrintStream(new BufferedOutputStream(ps));
                os.println("I have a String here");
    
                SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    
                parser.parse((InputSteam)is, new SAXmyHtmlHandler(document));
    My problem is The SAXParser can only read from an InputStream. I need to pass an InputStream to it. But the data i have is in String format. So how can i convert the String to the InputString so that it can read by the SAXParser

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Re: Write String to InputStream

    The code does not work. It just my assumption. Thanks for helping. I really need the answer.

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Write String to InputStream

    There is no java class with the name SAXmyHtmlHandler
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Re: Write String to InputStream

    You can google it the first link will be. Thanks for helping. I really want to know when i have a String. How can i make it readable in a InputStream.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Write String to InputStream

    I traced the application
    In the PipedInputStream I found this:
    Code:
    public synchronized int read () throws IOException {
        if (!connected) {
          throw new IOException("Pipe not connected") ;
        }
        else if (closedByReader) {
          throw new IOException("Pipe closed") ;
        }
        else if (writeSide != null && !writeSide.isAlive() && !closedByWriter &&
                 (in < 0)) {
          throw new IOException("Write end dead") ;
        }
    
        readSide = Thread.currentThread() ;
        int trials = 2 ;
        while (in < 0) {
          if (closedByWriter) {
            /* closed by writer, return EOF */
            return -1 ;
          }
          if ((writeSide != null) && (!writeSide.isAlive()) && (--trials < 0)) {
            throw new IOException("Pipe broken") ;
          }
          /* might be a writer waiting */
          notifyAll() ;
          try {
    -----------------------------------------------------------------
            /* This Line is your problem
              it means, your input stream isn't reading for some reason */
            wait(1000) ;
          }
          catch (InterruptedException ex) {
            throw new java.io.InterruptedIOException() ;
          }
        }
        int ret = buffer[out++] & 0xFF ;
        if (out >= buffer.length) {
          out = 0 ;
        }
        if (in == out) {
          /* now empty */
          in = -1 ;
        }
        return ret ;
      }
    I'll check it out and get back to you
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Re: Write String to InputStream

    Wow, you are really expert! Never thought that the error can be trace like that. Its not necessary to use the Piped connection. My main problem is I want to convert my String into the InputStream that are able to read by the SAXParser. i have a solution which is writing the String into a file and read it back. Cause the SAXParser also recieve File object. The code is like this:
    Code:
    	FileOutputStream fos = new FileOutputStream("new");
    	PrintStream ps = new PrintStream(new BufferedOutputStream(fos));
    	ps.print("my string goes here");
    	ps.close();
    	fos.close();
                SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
                parser.parse(new File("new"), new SAXmyHtmlHandler(document));
    This is able to work but my program did not allowed to create any file.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Talking Re: Write String to InputStream

    Thanks for helping. I've just found the solution.
    Code:
                StringBufferInputStream bis = new StringBufferInputStream(statement);
    Cause StringBufferInputStream is a class extends InputStream so thats it!!

  11. #11
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Write String to InputStream

    How did you try to create the file?
    try using
    Code:
    File.createTempFile("FileName","Extension");
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  12. #12
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Write String to InputStream

    Quote Originally Posted by chthong
    Thanks for helping. I've just found the solution.
    Code:
                StringBufferInputStream bis = new StringBufferInputStream(statement);
    Cause StringBufferInputStream is a class extends InputStream so thats it!!
    This class is deprecated, you better not use it
    use the ByteArrayInputStream class instead
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Re: Write String to InputStream

    1. The the first parameter accepted by parser required an input stream instead of string
    2. So i just create an input stream and pass it in
    Code:
    StringBufferInputStream bis = new StringBufferInputStream(statement);
    
    parser.parse((bis, new SAXmyHtmlHandler(document));

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Write String to InputStream

    I'll echo ComputerJy: StringBufferInputStream is deprecated. Don't use it.


    Don't use a ByteArrayInputStream either. Use a StringReader and create an org.xml.sax.InputSource from it.
    Code:
    import java.io.StringReader;
    import org.xml.sax.InputSource;
    
    ...
    parser.parse(new InputSource(new StringReader(statement)), handler);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    64

    Re: Write String to InputStream

    Thanks, Is that the meaning of deprecated? I will becarefull with it.

    Code:
    deprecated  - This term is used to refer to obsolete structures that should not be used for new applications but remain valid.

  16. #16
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Write String to InputStream

    Quote Originally Posted by chthong
    Thanks, Is that the meaning of deprecated? I will becarefull with it.

    Code:
    deprecated  - This term is used to refer to obsolete structures that should not be used for new applications but remain valid.
    Yes, this is the definition of deprecated
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width