41 |
import gnu.classpath.Configuration; |
import gnu.classpath.Configuration; |
42 |
|
|
43 |
/** |
/** |
44 |
* This class represents an opaque file handle as a Java class. It should |
* This class represents an opaque file handle as a Java class. It should |
45 |
* be used only to pass to other methods that expect an object of this |
* be used only to pass to other methods that expect an object of this |
46 |
* type. No system specific information can be obtained from this object. |
* type. No system specific information can be obtained from this object. |
47 |
* |
* |
48 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
49 |
*/ |
*/ |
50 |
public final class FileDescriptor |
public final class FileDescriptor |
51 |
{ |
{ |
52 |
|
static |
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Class Variables and Initializers |
|
|
*/ |
|
|
|
|
|
static |
|
53 |
{ |
{ |
54 |
if (Configuration.INIT_LOAD_LIBRARY) |
if (Configuration.INIT_LOAD_LIBRARY) |
55 |
{ |
{ |
74 |
static final int END; |
static final int END; |
75 |
|
|
76 |
/** |
/** |
77 |
* A <code>FileDescriptor</code> representing the system standard input |
* A <code>FileDescriptor</code> representing the system standard input |
78 |
* stream. This will usually be accessed through the |
* stream. This will usually be accessed through the |
79 |
* <code>System.in</code>variable. |
* <code>System.in</code>variable. |
80 |
*/ |
*/ |
81 |
public static final FileDescriptor in; |
public static final FileDescriptor in; |
82 |
|
|
83 |
/** |
/** |
84 |
* A <code>FileDescriptor</code> representing the system standard output |
* A <code>FileDescriptor</code> representing the system standard output |
85 |
* stream. This will usually be accessed through the |
* stream. This will usually be accessed through the |
86 |
* <code>System.out</code>variable. |
* <code>System.out</code>variable. |
87 |
*/ |
*/ |
88 |
public static final FileDescriptor out; |
public static final FileDescriptor out; |
89 |
|
|
90 |
/** |
/** |
91 |
* A <code>FileDescriptor</code> representing the system standard error |
* A <code>FileDescriptor</code> representing the system standard error |
92 |
* stream. This will usually be accessed through the |
* stream. This will usually be accessed through the |
93 |
* <code>System.err</code>variable. |
* <code>System.err</code>variable. |
94 |
*/ |
*/ |
95 |
public static final FileDescriptor err; |
public static final FileDescriptor err; |
96 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Instance Variables |
|
|
*/ |
|
|
|
|
97 |
/** |
/** |
98 |
* This is the actual native file descriptor value |
* This is the actual native file descriptor value |
|
*/ |
|
|
private long nativeFd = -1L; |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
99 |
*/ |
*/ |
100 |
|
private long nativeFd = -1L; |
101 |
|
|
102 |
/** |
/** |
103 |
* This method is used to initialize an invalid FileDescriptor object. |
* This method is used to initialize an invalid FileDescriptor object. |
104 |
*/ |
*/ |
105 |
public FileDescriptor() |
public FileDescriptor() |
106 |
{ |
{ |
107 |
} |
} |
111 |
this.nativeFd = nativeFd; |
this.nativeFd = nativeFd; |
112 |
} |
} |
113 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
114 |
/** |
/** |
115 |
* This method forces all data that has not yet been physically written to |
* This method forces all data that has not yet been physically written to |
116 |
* the underlying storage medium associated with this |
* the underlying storage medium associated with this |
117 |
* <code>FileDescriptor</code> |
* <code>FileDescriptor</code> |
118 |
* to be written out. This method will not return until all data has |
* to be written out. This method will not return until all data has |
119 |
* been fully written to the underlying device. If the device does not |
* been fully written to the underlying device. If the device does not |
120 |
* support this functionality or if an error occurs, then an exception |
* support this functionality or if an error occurs, then an exception |
121 |
* will be thrown. |
* will be thrown. |
122 |
*/ |
*/ |
123 |
public void sync() throws SyncFailedException |
public void sync() throws SyncFailedException |
124 |
{ |
{ |
125 |
if (nativeFd == -1L) |
if (nativeFd == -1L) |
129 |
} |
} |
130 |
|
|
131 |
/** |
/** |
132 |
* This methods tests whether or not this object represents a valid open |
* This methods tests whether or not this object represents a valid open |
133 |
* native file handle. |
* native file handle. |
134 |
* |
* |
135 |
* @return <code>true</code> if this object represents a valid |
* @return <code>true</code> if this object represents a valid |
136 |
* native file handle, <code>false</code> otherwise |
* native file handle, <code>false</code> otherwise |
137 |
*/ |
*/ |
138 |
public boolean valid() |
public boolean valid() |
139 |
{ |
{ |
140 |
if (nativeFd == -1L) |
if (nativeFd == -1L) |
299 |
close(); |
close(); |
300 |
} |
} |
301 |
|
|
|
/*************************************************************************/ |
|
|
|
|
302 |
/* |
/* |
303 |
* Native FileDescriptor provider interface |
* Native FileDescriptor provider interface |
304 |
* |
* |
308 |
*/ |
*/ |
309 |
|
|
310 |
/** |
/** |
311 |
* This method is called in the class initializer to do any require |
* This method is called in the class initializer to do any require |
312 |
* native library initialization. It is also responsible for initializing |
* native library initialization. It is also responsible for initializing |
313 |
* the in, out, and err variables. |
* the in, out, and err variables. |
314 |
*/ |
*/ |
315 |
private static native void nativeInit(); |
private static native void nativeInit(); |
316 |
|
|
317 |
/** |
/** |
318 |
* Opens the specified file in the specified mode. This can be done |
* Opens the specified file in the specified mode. This can be done |
319 |
* in one of the specified modes: |
* in one of the specified modes: |
320 |
* <ul> |
* <ul> |
321 |
* <li>r - Read Only |
* <li>r - Read Only |
322 |
* <li>rw - Read / Write |
* <li>rw - Read / Write |
323 |
* <li>ra - Read / Write - append to end of file |
* <li>ra - Read / Write - append to end of file |
324 |
* <li>rws - Read / Write - synchronous writes of data/metadata |
* <li>rws - Read / Write - synchronous writes of data/metadata |
325 |
* <li>rwd - Read / Write - synchronous writes of data. |
* <li>rwd - Read / Write - synchronous writes of data. |
326 |
* |
* |
327 |
* @param path Name of the file to open |
* @param path Name of the file to open |
328 |
* @param mode Mode to open |
* @param mode Mode to open |
329 |
* |
* |
330 |
* @returns The resulting file descriptor for the opened file, or -1 |
* @returns The resulting file descriptor for the opened file, or -1 |
331 |
* on failure (exception also signaled). |
* on failure (exception also signaled). |
332 |
* |
* |
333 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
334 |
*/ |
*/ |
335 |
private native long nativeOpen(String path, String mode) throws IOException; |
private native long nativeOpen(String path, String mode) throws IOException; |
336 |
|
|
337 |
/** |
/** |
338 |
* Closes this specified file descriptor |
* Closes this specified file descriptor |
339 |
* |
* |
340 |
* @param fd The native file descriptor to close |
* @param fd The native file descriptor to close |
341 |
* |
* |
342 |
* @returns The return code of the native close command. |
* @returns The return code of the native close command. |
343 |
* |
* |
344 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
345 |
*/ |
*/ |
346 |
private native long nativeClose(long fd) throws IOException; |
private native long nativeClose(long fd) throws IOException; |
347 |
|
|
348 |
/** |
/** |
349 |
* Writes a single byte to the file |
* Writes a single byte to the file |
350 |
* |
* |
351 |
* @param fd The native file descriptor to write to |
* @param fd The native file descriptor to write to |
352 |
* @param b The byte to write, encoded in the low eight bits |
* @param b The byte to write, encoded in the low eight bits |
353 |
* |
* |
354 |
* @return The return code of the native write command |
* @return The return code of the native write command |
355 |
* |
* |
356 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
357 |
*/ |
*/ |
358 |
private native long nativeWriteByte(long fd, int b) throws IOException; |
private native long nativeWriteByte(long fd, int b) throws IOException; |
359 |
// I hate name mangling. |
// I hate name mangling. |
360 |
|
|
361 |
/** |
/** |
362 |
* Writes a byte buffer to the file |
* Writes a byte buffer to the file |
363 |
* |
* |
364 |
* @param fd The native file descriptor to write to |
* @param fd The native file descriptor to write to |
365 |
* @param buf The byte buffer to write from |
* @param buf The byte buffer to write from |
366 |
* @param int The offset into the buffer to start writing from |
* @param int The offset into the buffer to start writing from |
367 |
* @param len The number of bytes to write. |
* @param len The number of bytes to write. |
368 |
* |
* |
369 |
* @return The return code of the native write command |
* @return The return code of the native write command |
370 |
* |
* |
371 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
372 |
*/ |
*/ |
373 |
private native long nativeWriteBuf(long fd, byte[] buf, int offset, int len) |
private native long nativeWriteBuf(long fd, byte[] buf, int offset, int len) |
374 |
throws IOException; |
throws IOException; |
375 |
// I hate name mangling. |
// I hate name mangling. |
388 |
// I hate name mangling. |
// I hate name mangling. |
389 |
|
|
390 |
/** |
/** |
391 |
* Reads a buffer of bytes from the file |
* Reads a buffer of bytes from the file |
392 |
* |
* |
393 |
* @param fd The native file descriptor to read from |
* @param fd The native file descriptor to read from |
394 |
* @param buf The buffer to read bytes into |
* @param buf The buffer to read bytes into |
395 |
* @param offset The offset into the buffer to start storing bytes |
* @param offset The offset into the buffer to start storing bytes |
396 |
* @param len The number of bytes to read. |
* @param len The number of bytes to read. |
397 |
* |
* |
398 |
* @return The number of bytes read, or -1 if end of file. |
* @return The number of bytes read, or -1 if end of file. |
399 |
* |
* |
400 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
401 |
*/ |
*/ |
402 |
private native int nativeReadBuf(long fd, byte[] buf, int offset, int len) |
private native int nativeReadBuf(long fd, byte[] buf, int offset, int len) |
403 |
throws IOException; |
throws IOException; |
404 |
// I hate name mangling. |
// I hate name mangling. |
405 |
|
|
406 |
/** |
/** |
407 |
* Returns the number of bytes available for reading |
* Returns the number of bytes available for reading |
408 |
* |
* |
409 |
* @param fd The native file descriptor |
* @param fd The native file descriptor |
410 |
* |
* |
411 |
* @return The number of bytes available for reading |
* @return The number of bytes available for reading |
412 |
* |
* |
413 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
414 |
*/ |
*/ |
415 |
private native int nativeAvailable(long fd) throws IOException; |
private native int nativeAvailable(long fd) throws IOException; |
416 |
|
|
417 |
/** |
/** |
418 |
* Method to do a "seek" operation on the file |
* Method to do a "seek" operation on the file |
419 |
* |
* |
420 |
* @param fd The native file descriptor |
* @param fd The native file descriptor |
421 |
* @param offset The number of bytes to seek |
* @param offset The number of bytes to seek |
422 |
* @param whence The position to seek from, either |
* @param whence The position to seek from, either |
423 |
* SET (0) for the beginning of the file, CUR (1) for the |
* SET (0) for the beginning of the file, CUR (1) for the |
424 |
* current position or END (2) for the end position. |
* current position or END (2) for the end position. |
425 |
* @param stopAtEof <code>true</code> to ensure that there is no |
* @param stopAtEof <code>true</code> to ensure that there is no |
426 |
* seeking past the end of the file, <code>false</code> otherwise. |
* seeking past the end of the file, <code>false</code> otherwise. |
427 |
* |
* |
428 |
* @return The new file position, or -1 if end of file. |
* @return The new file position, or -1 if end of file. |
429 |
* |
* |
430 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
431 |
*/ |
*/ |
432 |
private native long nativeSeek(long fd, long offset, int whence, |
private native long nativeSeek(long fd, long offset, int whence, |
433 |
boolean stopAtEof) |
boolean stopAtEof) |
434 |
throws IOException; |
throws IOException; |
435 |
|
|
436 |
/** |
/** |
437 |
* Returns the current position of the file pointer in the file |
* Returns the current position of the file pointer in the file |
438 |
* |
* |
439 |
* @param fd The native file descriptor |
* @param fd The native file descriptor |
440 |
* |
* |
441 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
442 |
*/ |
*/ |
443 |
private native long nativeGetFilePointer(long fd) throws IOException; |
private native long nativeGetFilePointer(long fd) throws IOException; |
444 |
// Really could implement in terms of seek |
// Really could implement in terms of seek |
445 |
|
|
446 |
/** |
/** |
447 |
* Returns the length of the file in bytes |
* Returns the length of the file in bytes |
448 |
* |
* |
449 |
* @param fd The native file descriptor |
* @param fd The native file descriptor |
450 |
* |
* |
451 |
* @return The length of the file in bytes |
* @return The length of the file in bytes |
452 |
* |
* |
453 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
454 |
*/ |
*/ |
455 |
private native long nativeGetLength(long fd) throws IOException; |
private native long nativeGetLength(long fd) throws IOException; |
456 |
|
|
457 |
/** |
/** |
458 |
* Sets the length of the file to the specified number of bytes |
* Sets the length of the file to the specified number of bytes |
459 |
* This can result in truncation or extension. |
* This can result in truncation or extension. |
460 |
* |
* |
461 |
* @param fd The native file descriptor |
* @param fd The native file descriptor |
462 |
* |
* @param len The new length of the file |
463 |
* @param len The new length of the file |
* |
464 |
* |
* @exception IOException If an error occurs |
465 |
* @exception IOException If an error occurs |
*/ |
|
*/ |
|
466 |
private native void nativeSetLength(long fd, long len) throws IOException; |
private native void nativeSetLength(long fd, long len) throws IOException; |
467 |
|
|
468 |
/** |
/** |
469 |
* Tests a file descriptor for validity |
* Tests a file descriptor for validity |
470 |
* |
* |
471 |
* @param fd The native file descriptor |
* @param fd The native file descriptor |
472 |
* |
* |
473 |
* @return <code>true</code> if the fd is valid, <code>false</code> |
* @return <code>true</code> if the fd is valid, <code>false</code> |
474 |
* otherwise |
* otherwise |
475 |
* |
* |
476 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
477 |
*/ |
*/ |
478 |
private native boolean nativeValid(long fd) throws IOException; |
private native boolean nativeValid(long fd) throws IOException; |
479 |
|
|
480 |
/** |
/** |
481 |
* Flushes any buffered contents to disk |
* Flushes any buffered contents to disk |
482 |
* |
* |
483 |
* @param fd The native file descriptor |
* @param fd The native file descriptor |
484 |
* |
* |
485 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
486 |
*/ |
*/ |
487 |
private native void nativeSync(long fd) throws SyncFailedException; |
private native void nativeSync(long fd) throws SyncFailedException; |
488 |
|
|
489 |
} // class FileDescriptor |
} // class FileDescriptor |