Created
April 1, 2012 01:27
-
-
Save laclefyoshi/2270355 to your computer and use it in GitHub Desktop.
handler for URL:sum://
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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