KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > InPort


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 JavaDoc 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 JavaDoc name)
25   {
26     this (in);
27     setName(name);
28   }
29
30   public static Reader convertToReader (InputStream in, Object JavaDoc conv)
31   {
32     if (conv != null && conv != Boolean.TRUE)
33       {
34     String JavaDoc enc = (conv == Boolean.FALSE ? "8859_1" : conv.toString());
35     try
36       {
37         return new java.io.InputStreamReader JavaDoc(in, enc);
38       }
39     catch (java.io.UnsupportedEncodingException JavaDoc ex)
40       {
41         throw new RuntimeException JavaDoc("unknown character encoding: "+enc);
42       }
43       }
44     return new java.io.InputStreamReader JavaDoc(in);
45   }
46
47   public InPort (InputStream in, Object JavaDoc name, Object JavaDoc conv)
48     throws java.io.UnsupportedEncodingException JavaDoc
49   {
50     this (convertToReader(in, conv), name);
51     if (conv == Boolean.FALSE)
52       {
53     // Use a fixed-size buffer. This prevents really-long "lines"
54
// from causing the buffer to grow to accomodate them.
55
try
56       {
57         setBuffer(new char[2048]);
58       }
59     catch (java.io.IOException JavaDoc ex) { /* ignored */ }
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   /** Tests if a URL has a scheme.
82    * For convenience, we treat a 1-character "scheme" as an
83    * MS-DOS-style "drive letter" - i.e. not a scheme. */

84   public static boolean uriSchemeSpecified (String JavaDoc 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   /** Helper routine to get the scheme part of a URI.
97    * The scheme part is "http:" or "file:" or "ftp:" most commonly.
98    * This functions searches for the first ':' that doesn't follow a '/'.
99    * @return The length of the scheme component, not counting the colon,
100    * (or alternatively the index of the colon,), or -1 if the is no scheme.
101    */

102   public static int uriSchemeLength (String JavaDoc 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 JavaDoc fname)
119     throws java.io.IOException JavaDoc
120   {
121     java.io.InputStream JavaDoc strm = URI_utils.getInputStream(fname);
122     strm = new java.io.BufferedInputStream JavaDoc(strm);
123     return openFile(strm, fname);
124   }
125
126   public static InPort openFile(InputStream strm, Object JavaDoc fname)
127     throws java.io.UnsupportedEncodingException JavaDoc
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 JavaDoc name = getName();
137     if (name != null)
138       {
139     out.write(' ');
140     out.write(name);
141       }
142     out.write('>');
143   }
144 }
145
Popular Tags