Hey So I have the following code that is used by a client side application to play a video file that is stored in MongoDB(using GridFS).
My question here is does the code have to wait until every byte that makes up the video file is written into the stream, before sending the response back to the client?
Obviously if that was the case it would be highly undesirable as this consumes all bytes into memory, which means large files could bring down the server. The purpose of streaming is to avoid consuming all bytes into memory.
@Path("video/{videoId}")
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getTheSelectedVideo(@PathParam("videoId") String videoId){
GridFSDBFile videoFile = gridFS.findOne(new ObjectId(videoId));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return Response.ok(videoFile.writeTo(baos)).build();
}
catch (IOException e) {
e.printStackTrace();
return Response.serverError().build();
}
}
Note gridFS here refers to a GridFS object created further up in the code.
Any help on clearing this up would be great! Thanks.