atomically write the console note to the output stream

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@36972 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2010-11-16 00:56:59 +00:00
1 parent b90fa3e202
commit 0fa4a00be0
1 file changed
+9 -5
@@ -151,20 +151,24 @@ public abstract class ConsoleNote<T> implements Serializable, Describable<Consol
* of {@link ConsoleAnnotator}.
*/
public void encodeTo(OutputStream out) throws IOException {
out.write(PREAMBLE);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(buf));
oos.writeObject(this);
oos.close();
DataOutputStream dos = new DataOutputStream(new Base64OutputStream(new FlushProofOutputStream(new CloseProofOutputStream(out)),true,-1,null));
// we don't need the size by ourselves, but it's useful to gracefully recover from an error
// if the deserialization fail in the middle.
ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2,true,-1,null));
dos.write(PREAMBLE);
dos.writeInt(buf.size());
buf.writeTo(dos);
dos.write(POSTAMBLE);
dos.close();
out.write(POSTAMBLE);
// atomically write to the final output, to minimize the chance of something else getting in between the output.
// even with this, it is still technically possible to get such a mix-up to occur (for example,
// if Java program is reading stdout/stderr separately and copying them into the same final stream.)
out.write(buf2.toByteArray());
}
/**