I would like to persist a sort of object using ObjectOutputStream into stringwriter as a last outoput data. If this was not clear check out this piece of code bellow.
StringWriter sw = new StringWriter();
OutputStream out = null ; which object wrap sw here?
try (ObjectOutputStream obj = new ObjectOutputStream(out)){
} catch (IOException e) {
e.printStackTrace();
}
As you can see which class should I use to wrap an object of Stringwriter.
Here's a working snippet of code:
// Create a base 64 encoded string and print it
byte[] data = {-1,-2,-3,0,1,2,3,};
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream enc = new ObjectOutputStream( bos );
enc.writeObject( data );
enc.close();
String b64 = Base64.getEncoder().encodeToString( bos.toByteArray() );
System.out.println( "data=" + b64 );
Don't use a StringWriter, use a ByteArrayOutputStream. The Writer interface and the OutputStream interface are two different things and you can't mix them up.
Related
Ultimately the goal is just to zip a string and have that as a string in java
public void zip() {
try {
String myTestString = "a zip file test and another test";
InputStream in = new ByteArrayInputStream(myTestString.getBytes());
OutputStream out = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(out);
ZipEntry zipEntry = new ZipEntry("wtf.txt");
zipOut.putNextEntry(zipEntry);
zipOut.write(myTestString.getBytes(),0,myTestString.getBytes().length);
zipOut.close();
myTestString = out.toString();
out.close();
System.out.println(myTestString);
// just a test if I can read the file
in = new ByteArrayInputStream(myTestString.getBytes());
out = new FileOutputStream("c:\\t\\string.zip");
byte[] allBytes = new byte[(int) myTestString.getBytes().length];
in.read(allBytes);
out.write(allBytes);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I have found I can write a string to a zipfile using
public void zipStringToFile2() {
try {
FileOutputStream fos = new FileOutputStream("c:/t/compressed.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
String myTestString = "a zip file test and another test";
int buffer = myTestString.getBytes().length;
// byte[] myBytes = myTestString.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream(buffer);
ZipEntry zipEntry = new ZipEntry("wtf.txt");
zipOut.putNextEntry(zipEntry);
zipOut.write(myTestString.getBytes(),0,buffer);
zipOut.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
but I cannot get the output of zipOut to write to a string and then have the string write to a file and then via the OS open the zipfile. How can that be done?
myTestString = out.toString();
This doesn't do what you want. bytes aren't strings. toString() doesn't give useful information (it is a debugging tool). Keep the byte array (.toByteArray()).
out.close();
close the stream after you retrieved the data? Don't do that. Close first. (not that it matters, here. ByteArrayXStream's close() doesn't do anything at all. The point is, it either does nothing in which case you should remove it, or if it does have an effect, your code would be broken).
myTestString.getBytes()
No, don't ever call that method. It gives you the bytes by decoding the characters into bytes using 'platform default encoding'. Who knows what that is.
} catch (IOException e) {
e.printStackTrace();
}
The correct 'I dunno' exception handler is throw new RuntimeException("unhandled", e);, not e.printStackTrace();. You get more info, and you get fewer WTFs (because yours will continue execution even though things are clearly wrong, which is a very bad idea).
but I cannot get the output of zipOut to write to a string
Yup. Strings aren't bytes, what you want is not possible at all. In any language. Some languages make it look like you can - those are bad languages, that conflate strings and byte arrays. Python decided to crash and burn and invent a whole new python (python2k -> python3k) to try to fix this, which goes to show. Boy that was a ton of pain and they suffered it to fix this oversight.
but I cannot get the output of zipOut to write to a string and then have the string write to a file and then via the OS open the zipfile. How can that be done?
So, replace all occurences of 'string' in that sentence with 'byte array' and all is peachy fine!
this is really what I was looking for:
public void zip() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());
System.out.println("testString " + testString + " testString.getBytes() " + testString.getBytes());
ZipEntry zipEntry = new ZipEntry("Results.xml");
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = bais.read(bytes)) >= 0 ) {
zipOut.write(bytes, 0, length);
}
zipOut.close();
bais.close();
baos.close();
byte[] zipBytes = baos.toByteArray();
// just a test to see if can be opened in the operating system
OutputStream os = new FileOutputStream(new File("c:/t/again.zip"));
os.write(zipBytes);
os.close();
} catch(IOException e) {
e.printStackTrace();
}
}
I am trying to serialize an object into a ZipEntry using an ObjectOutputStream, however it doesn't appear to be writing anything because when I print the byte array produced, it shows null. I tried writing a string with the ZipOutputStream, and upon printing the resulting byte array got a sizeable result. SO my question is: why is the objectoutput stream not correctly writing into the ZipEntry. (ConfigEntry does implement Serializable).
String s = "Tired, Exhausted";
ConfigEntry con = new ConfigEntry("rand", "random", 3);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry("test.txt");
ObjectOutputStream obs = new ObjectOutputStream(zos);
zos.putNextEntry(entry);
obs.writeObject(con);
obs.close();
zos.closeEntry();
zos.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
os = bs.getOutputStream();
byte[] result = baos.toByteArray();
String test = new String(result, "UTF-8");
Log.v("Mac Address", test);
Log.v("Mac Address", Arrays.toString(result));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
This baos goes out of scope after the try block. You are writing to one baos and you are looking into another baos declared in an outer scope, probably an instance member of the class.
Is there any way to write DataOutputStream content to an Array or a String regardless which type of data it contains?
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(String dataPath)));
Thanks
Use ByteArrrayOutputStream.
https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = new DataOutputStream(baos);
os.write(...);
byte[] data = baos.toByteArray();
String dataAsString = new String(data, "UTF-8"); // or whatever encoding you are using
You may use the following strategy as well:
class CompositeOutputStream implements OutputStream {
private OutputStream first,second;
public CompositeOutputStream(OutputStream first, OutputStream second) {
this.first = first;
this.second=second;
}
public void write(int b) throws IOException {
first.write(b);
second.write(b);
}
// etc.
}
Use with:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = new CompositeOutputStream(new DataOutputStream(...), baos);
os.write(...);
byte[] data = baos.toByteArray();
String dataAsString = new String(data, "UTF-8"); // or whatever encoding you are using
// etc.
The "baos" is only a "mirror" of what's got written to your original DataOutputStream
You still need to handle exceptions correctly, and be carefull about the amount of data written (holding everything in memory may lead to out of memory), etc.
I am trying to test a program and for that I need to access ReadExternal function but I am getting StreamCorrupted exception on ObjectInputStream.
I know I need to use the object written by WriteObject but dont know how to do it...
ObjectOutputStream out=new ObjectOutputStream(new ByteArrayOutputStream());
out.writeObject(ss3);
ss3.writeExternal(out);
try{
ByteInputStream bi=new ByteInputStream();
bi.setBuf(bb);
out.write(bb);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bb));
String s1=(String) in.readObject();
}
catch(Exception e){
e.printStackTrace();
}
Apparently, you are trying to write the same object twice to the output stream:
out.writeObject(ss3);
ss3.writeExternal(out); // <-- Remove this!
The second write makes wrong use of the writeExternal() method, which should never be called explicitly but will be called by the ObjectOutputStream.
And: out.write(bb); tries to write the content of bb to the ObjectOutputStream. That's probably not what you want.
Try it like this:
// Create a buffer for the data generated:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream( bos );
out.writeObject(ss3);
// This makes sure the stream is written completely ('flushed'):
out.close();
// Retrieve the raw data written through the ObjectOutputStream:
byte[] data = bos.toByteArray();
// Wrap the raw data in an ObjectInputStream to read from:
ByteArrayInputStream bis = new ByteArrayInputStream( data );
ObjectInputStream in = new ObjectInputStream( bis );
// Read object(s) re-created from the raw data:
SomeClass obj = (SomeClass) in.readObject();
assert obj.equals( ss3 ); // optional ;-)
ss3.writeExternal(out);
You shouldn't be calling that method directly. You should be calling
out.writeObject(ss3);
I am trying to redirect System.out into a String using System.setOut, which takes a PrintStream. Is there any way to convert a StringWriter into a Stream so that I can pass it to setOut?
You can't do that exactly, since StringWriter is a Writer, not a Stream. But you can do this:
// create a ByteArray stream, which will be wrapped by a PrintStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);
// print whatever you got
String result = baos.toString();