1 package gnu.mapping; 2 import java.io.*; 3 import gnu.text.*; 4 import gnu.lists.Consumer; 5 6 public class InPort extends gnu.text.LineBufferedReader implements Printable 7 { 8 public InPort (Reader in) 9 { 10 super (in); 11 } 12 13 public InPort (Reader in, Object name) 14 { 15 this (in); 16 setName(name); 17 } 18 19 public InPort (InputStream in) 20 { 21 super (in); 22 } 23 24 public InPort (InputStream in, Object name) 25 { 26 this (in); 27 setName(name); 28 } 29 30 public static Reader convertToReader (InputStream in, Object conv) 31 { 32 if (conv != null && conv != Boolean.TRUE) 33 { 34 String enc = (conv == Boolean.FALSE ? "8859_1" : conv.toString()); 35 try 36 { 37 return new java.io.InputStreamReader (in, enc); 38 } 39 catch (java.io.UnsupportedEncodingException ex) 40 { 41 throw new RuntimeException ("unknown character encoding: "+enc); 42 } 43 } 44 return new java.io.InputStreamReader (in); 45 } 46 47 public InPort (InputStream in, Object name, Object conv) 48 throws java.io.UnsupportedEncodingException 49 { 50 this (convertToReader(in, conv), name); 51 if (conv == Boolean.FALSE) 52 { 53 try 56 { 57 setBuffer(new char[2048]); 58 } 59 catch (java.io.IOException ex) { } 60 } 61 else 62 setConvertCR(true); 63 } 64 65 private static InPort systemInPort 66 = new TtyInPort (System.in, "<stdin>", OutPort.outInitial); 67 public static final ThreadLocation inLocation 68 = new ThreadLocation("in-default"); 69 static { inLocation.setGlobal(systemInPort); } 70 71 static public InPort inDefault () 72 { 73 return (InPort) inLocation.get(); 74 } 75 76 static public void setInDefault (InPort in) 77 { 78 inLocation.set(in); 79 } 80 81 84 public static boolean uriSchemeSpecified (String name) 85 { 86 int ulen = uriSchemeLength(name); 87 if (ulen == 1 && File.separatorChar == '\\') 88 { 89 char drive = name.charAt(0); 90 return ! ((drive >= 'a' && drive <= 'z') 91 || (drive >= 'A' && drive <= 'Z')); 92 } 93 return ulen > 1; 94 } 95 96 102 public static int uriSchemeLength (String uri) 103 { 104 int len = uri.length(); 105 for (int i = 0; i < len; i++) 106 { 107 char ch = uri.charAt(i); 108 if (ch == ':') 109 return i; 110 if (i == 0 ? ! Character.isLetter(ch) 111 : (! Character.isLetterOrDigit(ch) 112 && ch != '+' && ch != '-' && ch != '.')) 113 return -1; 114 } 115 return -1; 116 } 117 118 public static InPort openFile(Object fname) 119 throws java.io.IOException 120 { 121 java.io.InputStream strm = URI_utils.getInputStream(fname); 122 strm = new java.io.BufferedInputStream (strm); 123 return openFile(strm, fname); 124 } 125 126 public static InPort openFile(InputStream strm, Object fname) 127 throws java.io.UnsupportedEncodingException 128 { 129 return new InPort(strm, fname, 130 Environment.user().get("port-char-encoding")); 131 } 132 133 public void print (Consumer out) 134 { 135 out.write("#<input-port"); 136 String name = getName(); 137 if (name != null) 138 { 139 out.write(' '); 140 out.write(name); 141 } 142 out.write('>'); 143 } 144 } 145 | Popular Tags |