Skip to content

Instantly share code, notes, and snippets.

@liweinan
Created August 4, 2014 10:29
Show Gist options
  • Save liweinan/17b26055aa1a67ffcc10 to your computer and use it in GitHub Desktop.
Save liweinan/17b26055aa1a67ffcc10 to your computer and use it in GitHub Desktop.
package org.jboss.resteasy.test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import static org.jboss.resteasy.test.TestPortProvider.generateURL;
/**
* @author <a href="mailto:l.weinan@gmail.com">Weinan Li</a>
*/
public class RESTEASY1081 {
@Path("/")
public static class MyResource {
@GET
@Path("stream")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public StreamingOutput stream() throws FileNotFoundException {
final FileInputStream file = new FileInputStream("/Volumes/D1/Software/Fedora-17-i386-DVD/Fedora-17-i386-DVD.iso");
return new StreamingOutput() {
@Override
public void write(OutputStream out) throws IOException, WebApplicationException {
//buffer size set to 10MB
int bufferSize = 1024 * 1024 * 10;
int buffer = 1;
int rs = file.read();
while (rs != -1) {
out.write(rs);
rs = file.read();
buffer++;
//flush the output stream every 10MB
if (buffer == bufferSize) {
buffer = 1;
out.flush();
}
}
out.flush();
out.close();
}
};
}
}
static Client client;
@BeforeClass
public static void setup() throws Exception {
NettyContainer.start().getRegistry().addPerRequestResource(MyResource.class);
client = ClientBuilder.newClient();
}
@AfterClass
public static void end() throws Exception {
try {
client.close();
} catch (Exception e) {
}
NettyContainer.stop();
}
@Test
public void test1081() throws Exception {
WebTarget target = client.target(generateURL("/stream"));
final Response response = target.request().get();
String out = response.readEntity(String.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment