How to convert String to OutputStream?
Hi,
I have a Java class
class util
{
public boolean convert(OutputStream out, InputStream);
}
I don't have access to the Java code, but only to the JAR file.
When I was getting the input and output from a file, this was
okay. Now I get the input from a String, and the output should
also be to a String.
Because convert() takes only streams, I am unable to pass Strings
to it. I can create temp files to get around this, but that would
slow down the process.
Is there a way to convert a String or a StringBuffer to an OutputStream
and InputStream?
Thanks in advance,
Aruna
Re: How to convert String to OutputStream?
Hi Aruna,
I am not quiet clear with what u want to do, but then the other way of doing
that is..u unjar the JAR files, get the class from that and using
JAD decompilor( u can download from net) convert the .class into .java( the
file that has the convert method) and override the convert method to take
Strings instead of streams. I dont know how much it makes sense for you.
but pls let me know if it worked out for u.
Satish
Re: How to convert String to OutputStream?
Hi Satish,
Reengineering in most cases would work. May be the code snippet
of class util{} in my posting made you think it was tiny little
class. I should have mentioned the classes in the JAR are actually
too complex to rewrite.
And hence I feel, may be the effort needed to create a I/O Stream
out of String is less than the effort involved to get the Java code
from .class files and make the change.
But I your idea about JAD decompiler is a good point, and I will
employ it in cases where that will be a time saver.
Thanks Satish,
Aruna
"satish" <satish_141@yahoo.com> wrote:
>
>Hi Aruna,
>
> I am not quiet clear with what u want to do, but then the other way of
doing
>that is..u unjar the JAR files, get the class from that and using
>JAD decompilor( u can download from net) convert the .class into .java(
the
>file that has the convert method) and override the convert method to take
>Strings instead of streams. I dont know how much it makes sense for you.
>but pls let me know if it worked out for u.
>
>Satish
Re: How to convert String to OutputStream?
Something like this should work as long as you are not talking about a lot
of data, otherwise I would say to write to a temporary file and read from
it after the convert operation.
/*
* IOString.java
*
* Created on August 24, 2002, 10:28 PM
*/
import java.io.*;
public class IOString
{
private StringBuffer buf;
/** Creates a new instance of IOString */
public IOString()
{
buf = new StringBuffer();
}
public IOString(String text)
{
buf = new StringBuffer(text);
}
public InputStream getInputStream()
{
return new IOString.IOStringInputStream();
}
public OutputStream getOutputStream()
{
return new IOString.IOStringOutputStream();
}
public String getString()
{
return buf.toString();
}
class IOStringInputStream extends java.io.InputStream
{
private int position = 0;
public int read() throws java.io.IOException
{
if (position<buf.length())
{
return buf.charAt(position++);
}else
{
return -1;
}
}
}
class IOStringOutputStream extends java.io.OutputStream
{
public void write(int character) throws java.io.IOException
{
buf.append((char)character);
}
}
public static void main (String[] args)
{
IOString target = new IOString();
IOString source = new IOString("Hello World.");
convert(target.getOutputStream(),source.getInputStream());
System.out.println(target.getString());
}
/** <CODE>convert</CODE> doesn't actual convert anything but copies byte
for byte
*/
public static boolean convert(java.io.OutputStream out, java.io.InputStream
in)
{
try
{
int r;
while ((r=in.read())!=-1)
{
out.write(r);
}
return true;
}catch (java.io.IOException ioe)
{
return false;
}
}
}
"Aruna" <arun_co@yahoo.com> wrote:
>
>
>Hi Satish,
>
>Reengineering in most cases would work. May be the code snippet
>of class util{} in my posting made you think it was tiny little
>class. I should have mentioned the classes in the JAR are actually
>too complex to rewrite.
>
>And hence I feel, may be the effort needed to create a I/O Stream
>out of String is less than the effort involved to get the Java code
>from .class files and make the change.
>
>But I your idea about JAD decompiler is a good point, and I will
>employ it in cases where that will be a time saver.
>
>Thanks Satish,
>Aruna
>
>
>"satish" <satish_141@yahoo.com> wrote:
>>
>>Hi Aruna,
>>
>> I am not quiet clear with what u want to do, but then the other way of
>doing
>>that is..u unjar the JAR files, get the class from that and using
>>JAD decompilor( u can download from net) convert the .class into .java(
>the
>>file that has the convert method) and override the convert method to take
>>Strings instead of streams. I dont know how much it makes sense for you.
>>but pls let me know if it worked out for u.
>>
>>Satish
>
Re: How to convert String to OutputStream?
This concept works!!!
The buffer copying to convert between reader/writer is there.
But there seems to be no better way.
Thanks a lot Joe.
Aruna
"Joe" <jtdelly@yahoo.com> wrote:
>
>Something like this should work as long as you are not talking about a lot
>of data, otherwise I would say to write to a temporary file and read from
>it after the convert operation.
>
>/*
> * IOString.java
> *
> * Created on August 24, 2002, 10:28 PM
> */
>
>import java.io.*;
>
>public class IOString
>{
> private StringBuffer buf;
> /** Creates a new instance of IOString */
> public IOString()
> {
> buf = new StringBuffer();
> }
> public IOString(String text)
> {
> buf = new StringBuffer(text);
> }
> public InputStream getInputStream()
> {
> return new IOString.IOStringInputStream();
> }
> public OutputStream getOutputStream()
> {
> return new IOString.IOStringOutputStream();
> }
> public String getString()
> {
> return buf.toString();
> }
> class IOStringInputStream extends java.io.InputStream
> {
> private int position = 0;
> public int read() throws java.io.IOException
> {
> if (position<buf.length())
> {
> return buf.charAt(position++);
> }else
> {
> return -1;
> }
> }
> }
> class IOStringOutputStream extends java.io.OutputStream
> {
> public void write(int character) throws java.io.IOException
> {
> buf.append((char)character);
> }
>
> }
>
> public static void main (String[] args)
> {
> IOString target = new IOString();
> IOString source = new IOString("Hello World.");
> convert(target.getOutputStream(),source.getInputStream());
> System.out.println(target.getString());
> }
>
> /** <CODE>convert</CODE> doesn't actual convert anything but copies
byte
>for byte
> */
> public static boolean convert(java.io.OutputStream out, java.io.InputStream
>in)
> {
> try
> {
> int r;
> while ((r=in.read())!=-1)
> {
> out.write(r);
> }
> return true;
> }catch (java.io.IOException ioe)
> {
> return false;
> }
> }
>}
>
>
>
>
>
>"Aruna" <arun_co@yahoo.com> wrote:
>>
>>
>>Hi Satish,
>>
>>Reengineering in most cases would work. May be the code snippet
>>of class util{} in my posting made you think it was tiny little
>>class. I should have mentioned the classes in the JAR are actually
>>too complex to rewrite.
>>
>>And hence I feel, may be the effort needed to create a I/O Stream
>>out of String is less than the effort involved to get the Java code
>>from .class files and make the change.
>>
>>But I your idea about JAD decompiler is a good point, and I will
>>employ it in cases where that will be a time saver.
>>
>>Thanks Satish,
>>Aruna
>>
>>
>>"satish" <satish_141@yahoo.com> wrote:
>>>
>>>Hi Aruna,
>>>
>>> I am not quiet clear with what u want to do, but then the other way of
>>doing
>>>that is..u unjar the JAR files, get the class from that and using
>>>JAD decompilor( u can download from net) convert the .class into .java(
>>the
>>>file that has the convert method) and override the convert method to take
>>>Strings instead of streams. I dont know how much it makes sense for you.
>>>but pls let me know if it worked out for u.
>>>
>>>Satish
>>
>
Re: How to convert String to OutputStream?
Hi,
I did the String conversion this way:
import java.io.*;
import java.util.*;
public class StringToOutputStream
{
public StringToOutputStream()
{
//Do and test the conversion
ByteArrayOutputStream out2 = (ByteArrayOutputStream) this.convert("This
is a test String");
System.out.println(out2.toString());
try
{
out2.close();
} catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
StringToOutputStream stringToOutputStream1 = new StringToOutputStream();
}
private OutputStream convert(String aString)
{
//Convert the string to a byte array
byte[] byteArray = aString.getBytes();
//Create a stream of that byte array
ByteArrayOutputStream out = new ByteArrayOutputStream(byteArray.length);
try
{
//Write the data to that stream
out.write(byteArray);
} catch(Exception e)
{
e.printStackTrace();
}
//Cast to OutputStream and return
return (OutputStream) out;
}
}
"Aruna" <arun_co@yahoo.com> wrote:
>
>
>Hi,
>
>I have a Java class
>
>class util
>{
> public boolean convert(OutputStream out, InputStream);
>}
>
>I don't have access to the Java code, but only to the JAR file.
>
>When I was getting the input and output from a file, this was
>okay. Now I get the input from a String, and the output should
>also be to a String.
>
>Because convert() takes only streams, I am unable to pass Strings
>to it. I can create temp files to get around this, but that would
>slow down the process.
>
>Is there a way to convert a String or a StringBuffer to an OutputStream
>and InputStream?
>
>Thanks in advance,
>Aruna
>
>