42 |
import java.io.BufferedOutputStream; |
import java.io.BufferedOutputStream; |
43 |
import java.io.File; |
import java.io.File; |
44 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
|
import java.io.FileNotFoundException; |
|
45 |
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
46 |
import java.io.FilePermission; |
import java.io.FilePermission; |
47 |
import java.io.InputStream; |
import java.io.InputStream; |
67 |
public class Connection extends URLConnection |
public class Connection extends URLConnection |
68 |
{ |
{ |
69 |
/** |
/** |
70 |
|
* Default permission for a file |
71 |
|
*/ |
72 |
|
private static final String DEFAULT_PERMISSION = "read"; |
73 |
|
|
74 |
|
/** |
75 |
* This is a File object for this connection |
* This is a File object for this connection |
76 |
*/ |
*/ |
77 |
private File file; |
private File file; |
79 |
/** |
/** |
80 |
* InputStream if we are reading from the file |
* InputStream if we are reading from the file |
81 |
*/ |
*/ |
82 |
private BufferedInputStream inputStream; |
private InputStream inputStream; |
83 |
|
|
84 |
/** |
/** |
85 |
* OutputStream if we are writing to the file |
* OutputStream if we are writing to the file |
86 |
*/ |
*/ |
87 |
private BufferedOutputStream outputStream; |
private OutputStream outputStream; |
88 |
|
|
89 |
/** |
/** |
90 |
* FilePermission to read the file |
* FilePermission to read the file |
92 |
private FilePermission permission; |
private FilePermission permission; |
93 |
|
|
94 |
/** |
/** |
|
* Default permission for a file |
|
|
*/ |
|
|
private static final String DEFAULT_PERMISSION = "read"; |
|
|
|
|
|
/** |
|
95 |
* Calls superclass constructor to initialize. |
* Calls superclass constructor to initialize. |
96 |
*/ |
*/ |
97 |
public Connection (URL url) |
public Connection(URL url) |
98 |
{ |
{ |
99 |
super (url); |
super (url); |
100 |
|
|
115 |
|
|
116 |
// If not connected, then file needs to be openned. |
// If not connected, then file needs to be openned. |
117 |
file = new File (getURL().getFile()); |
file = new File (getURL().getFile()); |
|
|
|
|
if (!file.exists()) |
|
|
throw new FileNotFoundException (file.getPath()); |
|
|
|
|
118 |
if (doInput) |
if (doInput) |
119 |
inputStream = new BufferedInputStream (new FileInputStream (file)); |
inputStream = new BufferedInputStream(new FileInputStream(file)); |
120 |
|
|
121 |
if (doOutput) |
if (doOutput) |
122 |
outputStream = new BufferedOutputStream (new FileOutputStream (file)); |
outputStream = new BufferedOutputStream(new FileOutputStream(file)); |
123 |
|
|
124 |
connected = true; |
connected = true; |
125 |
} |
} |
135 |
throws IOException |
throws IOException |
136 |
{ |
{ |
137 |
if (!doInput) |
if (!doInput) |
138 |
throw new ProtocolException ("Can't open InputStream if doInput is false"); |
throw new ProtocolException("Can't open InputStream if doInput is false"); |
139 |
|
|
140 |
if (!connected) |
if (!connected) |
141 |
connect(); |
connect(); |
154 |
throws IOException |
throws IOException |
155 |
{ |
{ |
156 |
if (!doOutput) |
if (!doOutput) |
157 |
throw new ProtocolException ("Can't open OutputStream if doOutput is false"); |
throw new |
158 |
|
ProtocolException("Can't open OutputStream if doOutput is false"); |
159 |
|
|
160 |
if (!connected) |
if (!connected) |
161 |
connect(); |
connect(); |
174 |
{ |
{ |
175 |
if (!connected) |
if (!connected) |
176 |
connect(); |
connect(); |
177 |
|
|
178 |
return file.lastModified(); |
return file.lastModified(); |
179 |
} |
} |
180 |
catch (IOException e) |
catch (IOException e) |
202 |
return -1; |
return -1; |
203 |
} |
} |
204 |
} |
} |
205 |
|
|
|
|
|
206 |
/** |
/** |
207 |
* This method returns a <code>Permission</code> object representing the |
* This method returns a <code>Permission</code> object representing the |
208 |
* permissions required to access this URL. This method returns a |
* permissions required to access this URL. This method returns a |
316 |
} |
} |
317 |
}; |
}; |
318 |
} |
} |
319 |
|
|
320 |
} // class FileURLConnection |
} // class FileURLConnection |