You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jens Skripczynski <sk...@myrealbox.com> on 2003/05/11 16:10:21 UTC

[ot] java.io creating InputStream without files

Hi,

I'm totally unfamiliar with java.io and I found only examples handling
URLConnection or file operations.

I want to simulate the InputStream of some html file (htmltext ="<html><body><a href="test">....</html>");
for a junit test (having a function public void testFunction (InputStream someStream);).

So I need some javaCode that accepts on one side some String (htmltext) and
creates an InputStream someStream. I thought I could somehow combine some
OutputStream with an InputStream, but found nothing how I can Combine some
OutputStream with an InputStream.

------------------------------ Imagination
public void testfunction (InputStream is){
   [...]
}
[...]
Sting htmltext="<html><body><a href="test.html>test</a></body></html>";

OutputStream os;
os.write(htmltext);

InputStream is = {magic}(os);
testfunction(is);
------------------------------

Ciao

Jens Skripczynski
-- 
E-Mail: skripi-lists(at)myrealbox(dot)com

Mit der Arroganz die intelligentesten Wesen dieser Erde zu sein,
setzen wir uns ueber die Dinge hinweg, die wir nicht verstehen.
gesehen im Karlshof, Darmstadt


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: [ot] java.io creating InputStream without files

Posted by Jens Skripczynski <sk...@myrealbox.com>.
Hi,

found out about Piped(Input|Output). This was the missing piece in the puzzle.

Have a nice sunday, (or start into the week).
 
Jens Skripczynski
-- 
E-Mail: skripi-lists(at)myrealbox(dot)com

There is a fantasy in Redmond that Microsoft products are innovative, but this
is based entirely on a peculiar confusion of the words »innovative« and
»successful«. Microsoft products are successful -- they make a lot of money --
but that doesn't make them innovative, or even particularly good.
                                -- Robert X. Cringely


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: [ot] java.io creating InputStream without files

Posted by Jacob Kjome <ho...@visi.com>.
I use the following for debugging in a GZip servlet filter.  It should 
provide an example for your case.  It gzip compresses the byte array and 
then decompresses it.....

byte[] origBytes = wrapper.toByteArray();
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
GZIPOutputStream gzout = new GZIPOutputStream(compressed);
gzout.write(origBytes);
gzout.finish();
gzout.close();

if (logger.isDebugEnabled()) {
     logger.debug("compressed data...\n"+compressed);
     ByteArrayInputStream bais = new 
ByteArrayInputStream(compressed.toByteArray());
     GZIPInputStream gzin = new GZIPInputStream(bais);
     byte[] buffer = new byte[1024];
     int n, i = 0, m = buffer.length;
     while ((n = gzin.read (buffer, i, m - i)) >= 0) {
         i += n;
         if (i >= m) {
             byte[] newBuffer = new byte[m *= 2];
             System.arraycopy (buffer, 0, newBuffer, 0, i);
             buffer = newBuffer;
         }
     }
     byte[] result = new byte[i];
     System.arraycopy (buffer, 0, result, 0, i);
     ByteArrayOutputStream decompressed = new ByteArrayOutputStream();
     DataOutputStream daos = new DataOutputStream(decompressed);
     daos.write(result);
     daos.flush();
     daos.close();
     logger.debug("decompressed data...\n"+decompressed);
}

Hope that helps,

Jake

At 04:10 PM 5/11/2003 +0200, you wrote:
>Hi,
>
>I'm totally unfamiliar with java.io and I found only examples handling
>URLConnection or file operations.
>
>I want to simulate the InputStream of some html file (htmltext 
>="<html><body><a href="test">....</html>");
>for a junit test (having a function public void testFunction (InputStream 
>someStream);).
>
>So I need some javaCode that accepts on one side some String (htmltext) and
>creates an InputStream someStream. I thought I could somehow combine some
>OutputStream with an InputStream, but found nothing how I can Combine some
>OutputStream with an InputStream.
>
>------------------------------ Imagination
>public void testfunction (InputStream is){
>    [...]
>}
>[...]
>Sting htmltext="<html><body><a href="test.html>test</a></body></html>";
>
>OutputStream os;
>os.write(htmltext);
>
>InputStream is = {magic}(os);
>testfunction(is);
>------------------------------
>
>Ciao
>
>Jens Skripczynski
>--
>E-Mail: skripi-lists(at)myrealbox(dot)com
>
>Mit der Arroganz die intelligentesten Wesen dieser Erde zu sein,
>setzen wir uns ueber die Dinge hinweg, die wir nicht verstehen.
>gesehen im Karlshof, Darmstadt
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org