OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [net/] [protocol/] [file/] [Connection.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* FileURLConnection.java -- URLConnection class for "file" protocol
2
   Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.java.net.protocol.file;
39
 
40
import gnu.classpath.SystemProperties;
41
 
42
import java.io.BufferedInputStream;
43
import java.io.BufferedOutputStream;
44
import java.io.ByteArrayInputStream;
45
import java.io.ByteArrayOutputStream;
46
import java.io.File;
47
import java.io.FileInputStream;
48
import java.io.FileOutputStream;
49
import java.io.FilePermission;
50
import java.io.IOException;
51
import java.io.InputStream;
52
import java.io.OutputStream;
53
import java.io.OutputStreamWriter;
54
import java.io.Writer;
55
import java.net.ProtocolException;
56
import java.net.URL;
57
import java.net.URLConnection;
58
import java.security.Permission;
59
import java.text.SimpleDateFormat;
60
import java.util.Date;
61
import java.util.Locale;
62
import java.net.MalformedURLException;
63
 
64
/**
65
 * This subclass of java.net.URLConnection models a URLConnection via
66
 * the "file" protocol.
67
 *
68
 * @author Aaron M. Renn (arenn@urbanophile.com)
69
 * @author Nic Ferrier (nferrier@tapsellferrier.co.uk)
70
 * @author Warren Levy (warrenl@cygnus.com)
71
 */
72
public class Connection extends URLConnection
73
{
74
  /**
75
   * Default permission for a file
76
   */
77
  private static final String DEFAULT_PERMISSION = "read";
78
 
79
  private static class StaticData
80
  {
81
    /**
82
     * HTTP-style DateFormat, used to format the last-modified header.
83
     */
84
    static SimpleDateFormat dateFormat
85
      = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
86
                             new Locale ("En", "Us", "Unix"));
87
 
88
    static String lineSeparator =
89
      SystemProperties.getProperty("line.separator");
90
  }
91
 
92
 
93
  /**
94
   * This is a File object for this connection
95
   */
96
  private File file;
97
 
98
  /**
99
   * If a directory, contains a list of files in the directory.
100
   */
101
  private byte[] directoryListing;
102
 
103
  /**
104
   * InputStream if we are reading from the file
105
   */
106
  private InputStream inputStream;
107
 
108
  /**
109
   * OutputStream if we are writing to the file
110
   */
111
  private OutputStream outputStream;
112
 
113
  /**
114
   * FilePermission to read the file
115
   */
116
  private FilePermission permission;
117
 
118
  /**
119
   * Calls superclass constructor to initialize.
120
   */
121
  public Connection(URL url)
122
  {
123
    super (url);
124
 
125
    permission = new FilePermission(getURL().getFile(), DEFAULT_PERMISSION);
126
  }
127
 
128
  /**
129
   * Unquote "%" + hex quotes characters
130
   *
131
   * @param str The string to unquote or null.
132
   *
133
   * @return The unquoted string or null if str was null.
134
   *
135
   * @exception MalformedURLException If the given string contains invalid
136
   * escape sequences.
137
   *
138
   */
139
  public static String unquote(String str) throws MalformedURLException
140
  {
141
    if (str == null)
142
      return null;
143
 
144
    final int MAX_BYTES_PER_UTF_8_CHAR = 3;
145
    byte[] buf = new byte[str.length()*MAX_BYTES_PER_UTF_8_CHAR];
146
    int pos = 0;
147
    for (int i = 0; i < str.length(); i++)
148
      {
149
        char c = str.charAt(i);
150
        if (c == '%')
151
          {
152
            if (i + 2 >= str.length())
153
              throw new MalformedURLException(str + " : Invalid quoted character");
154
            int hi = Character.digit(str.charAt(++i), 16);
155
            int lo = Character.digit(str.charAt(++i), 16);
156
            if (lo < 0 || hi < 0)
157
              throw new MalformedURLException(str + " : Invalid quoted character");
158
            buf[pos++] = (byte) (hi * 16 + lo);
159
          }
160
        else if (c > 127) {
161
            try {
162
                byte [] c_as_bytes = Character.toString(c).getBytes("utf-8");
163
                final int c_length = c_as_bytes.length;
164
                System.arraycopy(c_as_bytes, 0, buf, pos, c_length);
165
                pos += c_length;
166
            }
167
            catch (java.io.UnsupportedEncodingException x2) {
168
                throw (Error) new InternalError().initCause(x2);
169
            }
170
        }
171
        else
172
          buf[pos++] = (byte) c;
173
      }
174
    try
175
      {
176
        return new String(buf, 0, pos, "utf-8");
177
      }
178
    catch (java.io.UnsupportedEncodingException x2)
179
      {
180
        throw (Error) new InternalError().initCause(x2);
181
      }
182
  }
183
 
184
  /**
185
   * "Connects" to the file by opening it.
186
   */
187
  public void connect() throws IOException
188
  {
189
    // Call is ignored if already connected.
190
    if (connected)
191
      return;
192
 
193
    // If not connected, then file needs to be openned.
194
    file = new File (unquote(getURL().getFile()));
195
 
196
    if (! file.isDirectory())
197
      {
198
        if (doInput)
199
          inputStream = new BufferedInputStream(new FileInputStream(file));
200
 
201
        if (doOutput)
202
          outputStream = new BufferedOutputStream(new FileOutputStream(file));
203
      }
204
    else
205
      {
206
        if (doInput)
207
          {
208
            inputStream = new ByteArrayInputStream(getDirectoryListing());
209
          }
210
 
211
        if (doOutput)
212
          throw new ProtocolException
213
            ("file: protocol does not support output on directories");
214
      }
215
 
216
    connected = true;
217
  }
218
 
219
  /**
220
   * Populates the <code>directoryListing</code> field with a byte array
221
   * containing a representation of the directory listing.
222
   */
223
  byte[] getDirectoryListing()
224
    throws IOException
225
  {
226
    if (directoryListing == null)
227
      {
228
        ByteArrayOutputStream sink = new ByteArrayOutputStream();
229
        // NB uses default character encoding for this system
230
        Writer writer = new OutputStreamWriter(sink);
231
 
232
        String[] files = file.list();
233
 
234
        for (int i = 0; i < files.length; i++)
235
          {
236
            writer.write(files[i]);
237
            writer.write(StaticData.lineSeparator);
238
          }
239
 
240
        directoryListing = sink.toByteArray();
241
      }
242
    return directoryListing;
243
  }
244
 
245
  /**
246
   * Opens the file for reading and returns a stream for it.
247
   *
248
   * @return An InputStream for this connection.
249
   *
250
   * @exception IOException If an error occurs
251
   */
252
  public InputStream getInputStream()
253
    throws IOException
254
  {
255
    if (!doInput)
256
      throw new ProtocolException("Can't open InputStream if doInput is false");
257
 
258
    if (!connected)
259
      connect();
260
 
261
    return inputStream;
262
  }
263
 
264
  /**
265
   * Opens the file for writing and returns a stream for it.
266
   *
267
   * @return An OutputStream for this connection.
268
   *
269
   * @exception IOException If an error occurs.
270
   */
271
  public OutputStream getOutputStream()
272
    throws IOException
273
  {
274
    if (!doOutput)
275
      throw new
276
        ProtocolException("Can't open OutputStream if doOutput is false");
277
 
278
    if (!connected)
279
      connect();
280
 
281
    return outputStream;
282
  }
283
 
284
  /**
285
   * Get the last modified time of the resource.
286
   *
287
   * @return the time since epoch that the resource was modified.
288
   */
289
  public long getLastModified()
290
  {
291
    try
292
      {
293
        if (!connected)
294
          connect();
295
 
296
        return file.lastModified();
297
      }
298
    catch (IOException e)
299
      {
300
        return -1;
301
      }
302
  }
303
 
304
  /**
305
   *  Get an http-style header field. Just handle a few common ones.
306
   */
307
  public String getHeaderField(String field)
308
  {
309
    try
310
      {
311
        if (!connected)
312
          connect();
313
 
314
        if (field.equals("content-type"))
315
          return guessContentTypeFromName(file.getName());
316
        else if (field.equals("content-length"))
317
          {
318
            if (file.isDirectory())
319
              {
320
                return Integer.toString(getContentLength());
321
              }
322
            return Long.toString(file.length());
323
          }
324
        else if (field.equals("last-modified"))
325
          {
326
            synchronized (StaticData.dateFormat)
327
              {
328
                return StaticData.dateFormat.format(
329
                        new Date(file.lastModified()));
330
              }
331
          }
332
      }
333
    catch (IOException e)
334
      {
335
        // Fall through.
336
      }
337
    return null;
338
  }
339
 
340
  /**
341
   * Get the length of content.
342
   *
343
   * @return the length of the content.
344
   */
345
  public int getContentLength()
346
  {
347
    try
348
      {
349
        if (!connected)
350
          connect();
351
 
352
        if (file.isDirectory())
353
          {
354
            return getDirectoryListing().length;
355
          }
356
        return (int) file.length();
357
      }
358
    catch (IOException e)
359
      {
360
        return -1;
361
      }
362
  }
363
 
364
  /**
365
   * This method returns a <code>Permission</code> object representing the
366
   * permissions required to access this URL.  This method returns a
367
   * <code>java.io.FilePermission</code> for the file's path with a read
368
   * permission.
369
   *
370
   * @return A Permission object
371
   */
372
  public Permission getPermission() throws IOException
373
  {
374
    return permission;
375
  }
376
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.