Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.commonjava.util.sidecar.config.ProxyConfiguration;
import org.commonjava.util.sidecar.interceptor.ExceptionHandler;
import org.commonjava.util.sidecar.interceptor.MetricsHandler;
import org.commonjava.util.sidecar.util.BufferStreamingOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -34,6 +35,7 @@
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -206,8 +208,8 @@ private Response convertProxyResp( HttpResponse<Buffer> resp )
} );
if ( resp.body() != null )
{
byte[] bytes = resp.body().getBytes();
builder.entity( bytes );
StreamingOutput so = new BufferStreamingOutput( resp );
builder.entity( so );
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.commonjava.util.sidecar.util;

import io.vertx.mutiny.core.buffer.Buffer;
import io.vertx.mutiny.ext.web.client.HttpResponse;
import org.apache.commons.io.output.CountingOutputStream;
import org.apache.commons.io.output.TeeOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class BufferStreamingOutput
implements StreamingOutput
{
private static final int bufSize = 10 * 1024 * 1024;

private final Logger logger = LoggerFactory.getLogger( getClass() );

private HttpResponse<Buffer> response;

private Supplier<OutputStream> cacheStreamSupplier;

public BufferStreamingOutput( HttpResponse<Buffer> response )
{
this.response = response;
}

@Override
public void write( OutputStream output ) throws IOException, WebApplicationException
{
OutputStream cacheStream = null;
try(CountingOutputStream cout = new CountingOutputStream( output ))
{
OutputStream out = cout;
if ( cacheStreamSupplier != null )
{
cacheStream = cacheStreamSupplier.get();
if ( cacheStream != null )
{
out = new TeeOutputStream( cacheStream, output );
}
}

Buffer buffer = response.bodyAsBuffer();
int total = buffer.length();
int transferred = 0;
while ( transferred < total )
{
int next = bufSize < total ? bufSize : total;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be moved out of the while loop as bufSize and total will never change? And maybe Math.min(bufSize, total) is more readable?

byte[] bytes = buffer.getBytes( transferred, next );
out.write( bytes );

transferred = next;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking the quit condition here for the while loop. There are two cases:

  1. For small files whose size < bufSize, so there will be only one loop and can quit successfully.
  2. For large files whose size > bufSize. In this case, the "total" will be the real file size(from the buffer.length), right? So that means the transferred will always be set to bufSize because of the "bufSize<total". Will this cause a dead loop as transferred will be less than total forever?

}
out.flush();
}
finally
{
if ( cacheStream != null )
{
try
{
cacheStream.close();
}
catch ( Exception e )
{
logger.error( "Failed to close cache stream: " + e.getMessage(), e );
}
}
}
}

public void setCacheStreamSupplier( Supplier<OutputStream> cacheStreamSupplier )
{
this.cacheStreamSupplier = cacheStreamSupplier;
}
}