Omnipresent Omnipresent - 1 year ago 79
Java Question

Thread race condition just hangs while using PipedOutputStream

I am using piped output streams to convert

because the AWS java sdk does not allow puting objects on S3 using

I'm using the code below, however, this will intermittently just hang. This code is in a web application. Currently there is no load on the application...I am just trying it out on my personal computer.

ByteArrayOutputStream os = new ByteArrayOutputStream();
PipedInputStream inpipe = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(inpipe);
try {
String xmpXml = "<dc:description>somedesc</dc:description>"
JpegXmpRewriter rewriter = new JpegXmpRewriter();
rewriter.updateXmpXml(isNew1,os, xmpXml);
new Thread(new Runnable() {
public void run () {
try {
// write the original OutputStream to the PipedOutputStream
println "starting writeto"
println "ending writeto"
} catch (IOException e) {
System.out.println("Some exception)
ObjectMetadata metadata1 = new ObjectMetadata();
client.putObject(new PutObjectRequest("test-bucket", "167_sample.jpg", inpipe, metadata1));
catch (Exception e) {
System.out.println("Some exception")
finally {

Answer Source

Instead of bothering with the complexities of starting another thread, instantiating two concurrent classes, and then passing data from thread to thread, all to solve nothing but a minor limitation in the provided JDK API, you should just create a simple specialization of the ByteArrayOutputStream:

class BetterByteArrayOutputStream extends ByteArrayOutputStream {
    public ByteArrayInputStream toInputStream() {
        return new ByteArrayInputStream(buf, 0, count);

This converts it to an input stream with no copying.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download