I'm trying to stream local mp3 files. The song I am testing is with 5:33s long. In both chrome and edge the song plays just under 4 minutes before it stops.
Here's my service
@Path ("clips")
public class ClipService {
@GET
@Path(...)
@Produces("audio/mp3")
public Response streamAudio() {
Clip clip = ...
StreamingOutput output = buildStreamingOutput(clip);
return Response.ok(output).header(HttpHeaders.CONTENT_LENGTH, clip.getLength()).build();
}
private StreamingOutput buildStreamingOutput(Clip clip)
throws Exception {
return new StreamingOutput() {
@Override
public void write(final OutputStream out)
throws IOException, WebApplicationException {
byte[] buffer = new byte[1024];
int bytesRead;
try (InputStream in = clip.getInputStream()) {
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.flush();
}
}
};
}
}
The clip class
public class Clip {
private InputStream inputStream; // The file
private long length; // File.length()
And the error i'm getting...
SEVERE: An I/O error has occurred while writing a response message entity to the container output stream.
org.glassfish.jersey.server.internal.process.MappableException: org.eclipse.jetty.io.EofException
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:92)
...
Caused by: org.eclipse.jetty.io.EofException
at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:200)
...
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:51)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:178)
... 67 more
I've tried setting ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER
to 0 in my AppConfig to no avail.