|
|
@@ -1,83 +1,126 @@
|
|
|
-/*
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- * @copyright Ranides Atterwim
|
|
|
- * @license WTFPL
|
|
|
- * @url http://ranides.net/projects/assira
|
|
|
- */
|
|
|
-package net.ranides.assira.io;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.Reader;
|
|
|
-import java.net.URI;
|
|
|
-import java.nio.CharBuffer;
|
|
|
-
|
|
|
-public class URIReader extends Reader {
|
|
|
-
|
|
|
- private final URI uri;
|
|
|
- private final Reader delegate;
|
|
|
-
|
|
|
- /**
|
|
|
- * Tworzy nowy obiekt powiązany z podaną nazwą pliku oraz readerem.
|
|
|
- * @param file
|
|
|
- * @param reader
|
|
|
- */
|
|
|
- public URIReader(URI uri, Reader reader) {
|
|
|
- this.uri = uri;
|
|
|
- this.delegate = reader;
|
|
|
- }
|
|
|
-
|
|
|
- public URI uri() {
|
|
|
- return uri;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int read(CharBuffer target) throws IOException {
|
|
|
- return delegate.read(target);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int read() throws IOException {
|
|
|
- return delegate.read();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int read(char[] cbuf) throws IOException {
|
|
|
- return delegate.read(cbuf);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int read(char[] cbuf, int off, int len) throws IOException {
|
|
|
- return delegate.read(cbuf, off, len);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public long skip(long n) throws IOException {
|
|
|
- return delegate.skip(n);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean ready() throws IOException {
|
|
|
- return delegate.ready();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean markSupported() {
|
|
|
- return delegate.markSupported();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mark(int readAheadLimit) throws IOException {
|
|
|
- delegate.mark(readAheadLimit);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void reset() throws IOException {
|
|
|
- delegate.reset();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void close() throws IOException {
|
|
|
- delegate.close();
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+package net.ranides.assira.io;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.Reader;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.CharBuffer;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import net.ranides.assira.text.Charsets;
|
|
|
+import net.ranides.assira.text.URIUtils;
|
|
|
+
|
|
|
+public class URIReader extends Reader {
|
|
|
+
|
|
|
+ private static final Pattern RE_CHARSET = Pattern.compile(";[ ]+charset=([^; ]+)");
|
|
|
+
|
|
|
+ private final URI uri;
|
|
|
+ private final Reader delegate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tworzy nowy obiekt powiązany z podaną nazwą pliku oraz readerem.
|
|
|
+ * @param file
|
|
|
+ * @param reader
|
|
|
+ */
|
|
|
+ public URIReader(URI uri, Reader reader) {
|
|
|
+ this.uri = uri;
|
|
|
+ this.delegate = reader;
|
|
|
+ }
|
|
|
+
|
|
|
+ // @todo (assira #3) URIStreams: URIReader
|
|
|
+ public static URIReader fromURI(URI uri) throws IOException {
|
|
|
+ switch(uri.getScheme()) {
|
|
|
+ case "file:": {
|
|
|
+ File file = URIUtils.getFile(uri);
|
|
|
+ Charset cs = Charset.forName(URIUtils.getParam(uri, "charset").orElse("UTF-8"));
|
|
|
+ return fromFile(file, cs);
|
|
|
+ }
|
|
|
+ default: {
|
|
|
+ URLConnection c = uri.toURL().openConnection();
|
|
|
+ c.connect();
|
|
|
+ Charset cs = getContentCharset(c.getContentType());
|
|
|
+ return new URIReader(uri, new InputStreamReader(c.getInputStream(), cs));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Charset getContentCharset(String type) {
|
|
|
+ if(null == type) {
|
|
|
+ return Charsets.UTF8;
|
|
|
+ }
|
|
|
+ Matcher hit = RE_CHARSET.matcher(type);
|
|
|
+ if(!hit.find()) {
|
|
|
+ return Charsets.UTF8;
|
|
|
+ }
|
|
|
+ return Charset.forName(hit.group(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static URIReader fromFile(File file, Charset cs) throws IOException {
|
|
|
+ return new URIReader(file.toURI(), new InputStreamReader(new FileInputStream(file),cs));
|
|
|
+ }
|
|
|
+
|
|
|
+ public URI uri() {
|
|
|
+ return uri;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int read(CharBuffer target) throws IOException {
|
|
|
+ return delegate.read(target);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int read() throws IOException {
|
|
|
+ return delegate.read();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int read(char[] cbuf) throws IOException {
|
|
|
+ return delegate.read(cbuf);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int read(char[] cbuf, int off, int len) throws IOException {
|
|
|
+ return delegate.read(cbuf, off, len);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long skip(long n) throws IOException {
|
|
|
+ return delegate.skip(n);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean ready() throws IOException {
|
|
|
+ return delegate.ready();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean markSupported() {
|
|
|
+ return delegate.markSupported();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mark(int readAheadLimit) throws IOException {
|
|
|
+ delegate.mark(readAheadLimit);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void reset() throws IOException {
|
|
|
+ delegate.reset();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void close() throws IOException {
|
|
|
+ delegate.close();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|