Created
April 1, 2012 02:09
-
-
Save laclefyoshi/2270603 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
#!/usr/bin/env jython | |
# -*- coding: utf-8 -*- | |
# Copyright : (c) SAEKI Yoshiyasu | |
# License : MIT-style license | |
# <http://www.opensource.org/licenses/mit-license.php> | |
# last updated: 2012/04/01 | |
from java.lang import String | |
from java.net import URL | |
from java.net import URLConnection | |
from java.net import URLStreamHandler | |
from java.net import URLStreamHandlerFactory | |
from java.io import ByteArrayInputStream | |
from java.io import InputStreamReader | |
class SumConnection(URLConnection): | |
def __init__(self, url): | |
self.__parent = super(SumConnection, self) | |
self.__parent.__init__(url) | |
def connect(self): | |
pass | |
def getInputStream(self): | |
return ByteArrayInputStream( | |
String(String.valueOf( | |
sum(map(int, self.__parent.getURL().getHost().split(","))))) | |
.getBytes()) | |
def getContentType(self): | |
return "text/plain" | |
class SumStreamHandler(URLStreamHandler): | |
def openConnection(self, url): | |
return SumConnection(url) | |
class SumStreamFactory(URLStreamHandlerFactory): | |
def createURLStreamHandler(self, protocol): | |
if protocol == "sum": | |
return SumStreamHandler() | |
else: | |
return None | |
URL.setURLStreamHandlerFactory(SumStreamFactory()) | |
if __name__ == "__main__": | |
u = URL("sum://11,22,33,44") | |
result = "" | |
isr = InputStreamReader(u.getContent()) | |
while isr.ready(): | |
result += chr(isr.read()) | |
print result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment