Java.net URLConnection.setContentHandlerFactory doesn't work?

815 Views Asked by At

I am attempting to solve a problem by extending URLStreamHandler with the goal of providing a file from memory. I have the class...

public class CustomURLStreamHandler extends URLStreamHandler {
    private Handler fileHandler = new Handler();

    @Override
    protected URLConnection openConnection(URL url) throws IOException {
        if (url.toString().contains(".mp4")) {
            return new CustomURLConnection(url);
        }

        return fileHandler.openConnection(url);
    }
}

...which is going to try to change the way MP4s are loaded. For example, say I have a really large video file and in order to save memory, I want to only have X bytes of it loaded at any given time. The CustomURLConnection is here...

public class CustomURLConnection extends URLConnection {
    public CustomURLConnection(URL url) {
        super(url);
    }

    @Override
    public void connect() throws IOException {
        // Nothing needed
    }

    @Override
    public synchronized java.io.InputStream getInputStream() throws IOException {
        return new CustomInputStream(this.url)));
    }
}

...but note that the getInputStream is not doing what it looks like. After much investigation, it seems that the URL connection's getInputStream is ONLY used to determine the content type of the media. In my case, it only looks at the first 22 bytes. If I change the method to...

@Override
public synchronized java.io.InputStream getInputStream() throws IOException {
    try {
        return new ByteInputStream(new byte[] {}, 0);
    }
}

...the file will still load... because this input stream is only being used to detect the content type and if it fails, Java will try to determine the content type using an unknown content handler at first that uses the URL and figures everything out. An important note is that I use my CustomURLStreamHandler and CustomURLConnection by changing my main method to...

public static void main(String[] args) {
    URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory());
    launch(args);
}

...and in the CustomURLStreamHandlerFactory, I use my CustomURLStreamHandler when the protocol is "file". It seems to me that the last piece of the puzzle is extend the content handler for the MP4 MIME.

Herein lies my problem, I changed my main method to...

public static void main(String[] args) {
    URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory());
    URLConnection.setContentHandlerFactory(new CustomContentHandlerFactory());
    launch(args);
}

...and I put a breakpoint in the overridden method "createContentHandler" but it NEVER gets hit. I tried this using the java.net and the sun.net.www URLConnection (although I am 99% sure the java.net one is the correct one to use). I am quite sure that if I can hook into the content handler I care about, I can get this working. My java version...

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

Has anyone had success supplying custom content handlers? Any help is much appreciated, thanks.

0

There are 0 best solutions below