Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created April 1, 2012 01:27
Show Gist options
  • Save laclefyoshi/2270355 to your computer and use it in GitHub Desktop.
Save laclefyoshi/2270355 to your computer and use it in GitHub Desktop.
handler for URL:sum://
/**
Copyright: (c) SAEKI Yoshiyasu
License : MIT-style license
<http://www.opensource.org/licenses/mit-license.php>
last updated: 2012/03/31
**/
import java.net.{URL, URLConnection, URLStreamHandler, URLStreamHandlerFactory}
import java.io.{InputStream, InputStreamReader, ByteArrayInputStream}
class SumConnection(url:URL) extends URLConnection(url:URL) {
override def connect = {}
override def getInputStream:ByteArrayInputStream = {
val value:Int = getURL.getHost.split(",").map(_.toInt).sum
new ByteArrayInputStream(value.toString.getBytes)
}
override def getContentType:String = "text/plain"
}
class SumStreamHandler extends URLStreamHandler {
override def openConnection(url:URL):URLConnection = new SumConnection(url)
}
class SumStreamHandlerFactory extends URLStreamHandlerFactory {
override def createURLStreamHandler(protocol:String) = {
protocol match {
case "sum" => new SumStreamHandler()
case _ => null
}
}
}
URL.setURLStreamHandlerFactory(new SumStreamHandlerFactory())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment