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.
Printable View
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.
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"?
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 SAXParserCode:
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));
The code does not work. It just my assumption. Thanks for helping. I really need the answer.
There is no java class with the name SAXmyHtmlHandler
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.
I traced the application
In the PipedInputStream I found this:
I'll check it out and get back to youCode: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 ;
}
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:
This is able to work but my program did not allowed to create any file. :(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));
Thanks for helping. I've just found the solution.
Cause StringBufferInputStream is a class extends InputStream so thats it!! :wave:Code:StringBufferInputStream bis = new StringBufferInputStream(statement);
How did you try to create the file?
try usingCode:File.createTempFile("FileName","Extension");
This class is deprecated, you better not use itQuote:
Originally Posted by chthong
use the ByteArrayInputStream class instead
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));
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);
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 deprecatedQuote:
Originally Posted by chthong