Source Home >> Java Source 1.6.0 >> java.io.FileDescriptor V 0.09
  • 001/*
  • 002 * @(#)FileDescriptor.java 1.4 05/11/17
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.io;
  • 009
  • 010/**
  • 011 * Instances of the file descriptor class serve as an opaque handle
  • 012 * to the underlying machine-specific structure representing an open
  • 013 * file, an open socket, or another source or sink of bytes. The
  • 014 * main practical use for a file descriptor is to create a
  • 015 * <code>FileInputStream</code> or <code>FileOutputStream</code> to
  • 016 * contain it.
  • 017 * <p>
  • 018 * Applications should not create their own file descriptors.
  • 019 *
  • 020 * @author Pavani Diwanji
  • 021 * @version 1.4, 11/17/05
  • 022 * @see java.io.FileInputStream
  • 023 * @see java.io.FileOutputStream
  • 024 * @since JDK1.0
  • 025 */
  • 026public final class FileDescriptor {
  • 027
  • 028 private int fd;
  • 029
  • 030 private long handle;
  • 031
  • 032 /**
  • 033 * Constructs an (invalid) FileDescriptor
  • 034 * object.
  • 035 */
  • 036 public /**/FileDescriptor() {
  • 037 fd = -1;
  • 038 handle = -1;
  • 039 }
  • 040
  • 041 private /* */FileDescriptor(int fd) {
  • 042 this.fd = fd;
  • 043 handle = -1;
  • 044 }
  • 045
  • 046 static {
  • 047 initIDs();
  • 048 }
  • 049
  • 050 /**
  • 051 * A handle to the standard input stream. Usually, this file
  • 052 * descriptor is not used directly, but rather via the input stream
  • 053 * known as <code>System.in</code>.
  • 054 *
  • 055 * @see java.lang.System#in
  • 056 */
  • 057 public static final FileDescriptor in = standardStream(0);
  • 058
  • 059 /**
  • 060 * A handle to the standard output stream. Usually, this file
  • 061 * descriptor is not used directly, but rather via the output stream
  • 062 * known as <code>System.out</code>.
  • 063 * @see java.lang.System#out
  • 064 */
  • 065 public static final FileDescriptor out = standardStream(1);
  • 066
  • 067 /**
  • 068 * A handle to the standard error stream. Usually, this file
  • 069 * descriptor is not used directly, but rather via the output stream
  • 070 * known as <code>System.err</code>.
  • 071 *
  • 072 * @see java.lang.System#err
  • 073 */
  • 074 public static final FileDescriptor err = standardStream(2);
  • 075
  • 076 /**
  • 077 * Tests if this file descriptor object is valid.
  • 078 *
  • 079 * @return <code>true</code> if the file descriptor object represents a
  • 080 * valid, open file, socket, or other active I/O connection;
  • 081 * <code>false</code> otherwise.
  • 082 */
  • 083 public boolean valid() {
  • 084 return ((handle != -1) || (fd != -1));
  • 085 }
  • 086
  • 087 /**
  • 088 * Force all system buffers to synchronize with the underlying
  • 089 * device. This method returns after all modified data and
  • 090 * attributes of this FileDescriptor have been written to the
  • 091 * relevant device(s). In particular, if this FileDescriptor
  • 092 * refers to a physical storage medium, such as a file in a file
  • 093 * system, sync will not return until all in-memory modified copies
  • 094 * of buffers associated with this FileDesecriptor have been
  • 095 * written to the physical medium.
  • 096 *
  • 097 * sync is meant to be used by code that requires physical
  • 098 * storage (such as a file) to be in a known state For
  • 099 * example, a class that provided a simple transaction facility
  • 100 * might use sync to ensure that all changes to a file caused
  • 101 * by a given transaction were recorded on a storage medium.
  • 102 *
  • 103 * sync only affects buffers downstream of this FileDescriptor. If
  • 104 * any in-memory buffering is being done by the application (for
  • 105 * example, by a BufferedOutputStream object), those buffers must
  • 106 * be flushed into the FileDescriptor (for example, by invoking
  • 107 * OutputStream.flush) before that data will be affected by sync.
  • 108 *
  • 109 * @exception SyncFailedException
  • 110 * Thrown when the buffers cannot be flushed,
  • 111 * or because the system cannot guarantee that all the
  • 112 * buffers have been synchronized with physical media.
  • 113 * @since JDK1.1
  • 114 */
  • 115 public native void sync() throws SyncFailedException;
  • 116
  • 117 /* This routine initializes JNI field offsets for the class */
  • 118 private static native void initIDs();
  • 119
  • 120 private static native long set(int d);
  • 121
  • 122 private static FileDescriptor standardStream(int fd) {
  • 123 FileDescriptor desc = new FileDescriptor();
  • 124 desc.handle = set(fd);
  • 125 return desc;
  • 126 }
  • 127
  • 128}

文件:FileDescriptor.java
包名:java.io
类名:FileDescriptor
继承:
接口: