-2

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.

TedTrippin
  • 3,525
  • 5
  • 28
  • 46
  • You don't have any information about who downvoted you, but have a look at the stack trace. The `EofException` was caused by the `IOException`, so the `IOException` is the actual problem here, so my edit was correct, and helpful, and your edit of my edit was not. Don't do that. I'm trying to help you. – user207421 Feb 04 '17 at 22:22
  • I will add for the benefit of the Jetty developers that end of file occurs when reading. Throwing an end of file on writing is ridiculous. It would have been better not to wrap the exception at all, or indeed not even to catch it. – user207421 Feb 04 '17 at 22:31
  • You need to stop guessing and start reading. I have clearly stated both the reason and the motivation for my edit, and what was wrong with your title. 'Lack of punctuation` is a ridiculous reason to reject the entire edit, which was copy/pasted directly from your stack trace. If you don't want your question answered by people who can explain the underlying `IOException`, you're going the right way about it. I've drawn this edit war to the attention of the moderators. – user207421 Feb 04 '17 at 22:41
  • Certainly not. I don't do chat; you seem to be just trying to continue the argument; and you've already insulted me once or twice. Have a look at my reputation here. I am certainly not in any need of inflating it via meaningless edits. As a matter of fact I don't *get* edit points, because of having enough reputation to bypass the edit approval queue. The accusation is merely bizarre. I've done quite enough here already. If you don't want your question answered, just keep ignoring and misconstruing everything you've been told here. – user207421 Feb 05 '17 at 02:23
  • That's enough of your crap for one question. Reported to moderators, again. Third time now. – user207421 Feb 05 '17 at 09:57
  • Third time? What happened to the second? Anyway, you could have at least answered my question instead of downvoting to disuade others from helping. Apologies for bruising your ego. – TedTrippin Feb 06 '17 at 02:17

1 Answers1

0

Here's an answer for those of you who don't wish to be obstructed by some pretentious what-not :)

Turns out the 2 browsers I tested with handle content-type=audio/mp3 by cancelling the original request and sending a second request with a range header.

Here's another post detailing how one can handle a range request.

Community
  • 1
  • 1
TedTrippin
  • 3,525
  • 5
  • 28
  • 46