Skip to content

Instantly share code, notes, and snippets.

@strogiyotec
Created November 30, 2021 22:38
Show Gist options
  • Save strogiyotec/b156e24137f5a1fcdf0260fb45a21b8f to your computer and use it in GitHub Desktop.
Save strogiyotec/b156e24137f5a1fcdf0260fb45a21b8f to your computer and use it in GitHub Desktop.
IO is not interrutable
executorService.submit(()->{
try(final InputStream in = new URL("https://r4---sn-h5576nsl.googlevideo.com/videoplayback?expire=1632366133&ei=1ZlLYY3-LonV1gLKsYHABw&ip=117.251.195.125&id=o-AP051py9A9yKVCFhcmizrLcgGyfmiZUnL_9qFpZNPk_i&itag=22&source=youtube&requiressl=yes&mh=JD&mm=31%2C26&mn=sn-h5576nsl%2Csn-cvh7knsz&ms=au%2Conr&mv=m&mvi=4&pl=24&initcwndbps=340000&vprv=1&mime=video%2Fmp4&ns=PUqKDKQGGAoseZDf2JzbU6sG&cnr=14&ratebypass=yes&dur=491.473&lmt=1511635732747562&mt=1632341355&fvip=4&fexp=24001373%2C24007246&beids=9466585&c=WEB&n=Gq1-qdUiiTgxwCWl&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAM8GmEHyVizy2D050utvqOvHiY_4XIoRICbHpsJDiWlNAiAwRkG1Ib7g_m_SpU9eF__PCfkzUo-l4Hpma8xOwdQ2-A%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgKAN11mH-4xDWCWtQKOoc2dMc8yzrGd622CHWrlmfUS0CIFO8EyO24gZYtrnsJIBbzahyQA_jYECvoeXfCH-THUW-&title=ADS1%3A%20The%20shortest%20common%20superstring%20problem").openStream()){
Files.copy(in, Paths.get("test.mp4"), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception exc){
exc.printStackTrace();
}
});
Thread.sleep(1000L);
executorService.shutdownNow();//this won't interrupt the task because InputStream uses read() syscall which can't be interrupted
// NIO is interruptable
final ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
try (
var readableByteChannel = Channels.newChannel(new URL("https://r4---sn-h5576nsl.googlevideo.com/videoplayback?expire=1632366133&ei=1ZlLYY3-LonV1gLKsYHABw&ip=117.251.195.125&id=o-AP051py9A9yKVCFhcmizrLcgGyfmiZUnL_9qFpZNPk_i&itag=22&source=youtube&requiressl=yes&mh=JD&mm=31%2C26&mn=sn-h5576nsl%2Csn-cvh7knsz&ms=au%2Conr&mv=m&mvi=4&pl=24&initcwndbps=340000&vprv=1&mime=video%2Fmp4&ns=PUqKDKQGGAoseZDf2JzbU6sG&cnr=14&ratebypass=yes&dur=491.473&lmt=1511635732747562&mt=1632341355&fvip=4&fexp=24001373%2C24007246&beids=9466585&c=WEB&n=Gq1-qdUiiTgxwCWl&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAM8GmEHyVizy2D050utvqOvHiY_4XIoRICbHpsJDiWlNAiAwRkG1Ib7g_m_SpU9eF__PCfkzUo-l4Hpma8xOwdQ2-A%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgKAN11mH-4xDWCWtQKOoc2dMc8yzrGd622CHWrlmfUS0CIFO8EyO24gZYtrnsJIBbzahyQA_jYECvoeXfCH-THUW-&title=ADS1%3A%20The%20shortest%20common%20superstring%20problem").openStream());
var fileOutputStream = new FileOutputStream("test.mp4")) {
try (final FileChannel fileChannel = fileOutputStream.getChannel()) {
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("HERE");
}
});
Thread.sleep(1000L);//it wil shutdown the task because nio uses non blocking system call
executorService.shutdownNow();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment