owfiledescriptor.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 1,991 行 · 第 1/5 页
JAVA
1,991 行
// create a compressed path (take out "." and "..") path = new Vector(verbosePath.size()); byte[] element; for (int element_num = 0; element_num < verbosePath.size(); element_num++) { element = (byte[]) verbosePath.elementAt(element_num); // ".." if ((element [0] == '.') && (element [1] == '.')) { // remove last entry in path if (path.size() > 0) path.removeElementAt(path.size() - 1); else { path = null; break; } } // not "." (so ignore entries ".") else if (element [0] != '.') { path.addElement(element); } } } } //-------- //-------- Standard FileDescriptor methods //-------- /** * Tests if this file descriptor object is valid. * * @return <code>true</code> if the file descriptor object represents a * valid, open file, socket, or other active I/O connection; * <code>false</code> otherwise. */ public boolean valid() { synchronized (cache) { return (cache != null); } } /** * Force all system buffers to synchronize with the underlying * device. This method returns after all modified data and * attributes of this FileDescriptor have been written to the * relevant device(s). In particular, if this FileDescriptor * refers to a physical storage medium, such as a file in a file * system, sync will not return until all in-memory modified copies * of buffers associated with this FileDesecriptor have been * written to the physical medium. * * sync is meant to be used by code that requires physical * storage (such as a file) to be in a known state For * example, a class that provided a simple transaction facility * might use sync to ensure that all changes to a file caused * by a given transaction were recorded on a storage medium. * * sync only affects buffers downstream of this FileDescriptor. If * any in-memory buffering is being done by the application (for * example, by a BufferedOutputStream object), those buffers must * be flushed into the OWFileDescriptor (for example, by invoking * OutputStream.flush) before that data will be affected by sync. * * <p>This method may be called multiple times if the source of * OWSyncFailedException has been rectified (1-Wire device was * reattached to the network). * * @exception OWSyncFailedException * Thrown when the buffers cannot be flushed, * or because the system cannot guarantee that all the * buffers have been synchronized with physical media. */ public void sync() throws OWSyncFailedException { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===sync"); try { synchronized (cache) { // clear last page read flag cache.clearLastPageRead(); // flush the writes to the device cache.sync(); } } catch (OneWireIOException e) { throw new OWSyncFailedException(e.toString()); } catch (OneWireException e) { throw new OWSyncFailedException(e.toString()); } } //-------- //-------- General File methods //-------- /** * Opens the file for reading. If successfull (no exceptions) then * the following class member variables will be set: * <ul> * <li> fePage - File Entry page number * <li> feOffset - Offset into File Entry page * <li> feData - buffer containing the last File Entry Page * <li> feLen - length of packet in the last File Entry Page * <li> feNumPages - Number of Pages specified in File Entry * <li> feStartPage - Start Page specified in the File Entry * <li> feParentPage - Parent page of current File Entry Page * <li> feParentOffset - Offset into Parent page * <li> lastPage - (file only) last page read * <li> lastOffset - (file only) offset into last page read * <li> lastLen - (file only) length of last page read * <li> lastPageData - (file only) buffer for the last page read * <li> filePosition - (file only) overall file position when reading * </ul> * * @throws FileNotFoundException when the file/directory path is invalid or * there was an IOException thrown when trying to read the device. */ protected void open() throws OWFileNotFoundException { String last_error = null; int cnt = 0; synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===open"); // clear last page read flag in the cache cache.clearLastPageRead(); // reset the file position lastPage = -1; // check if had an invalid path if (path == null) throw new OWFileNotFoundException("Invalid path"); // check if have an empty path if (path.size() == 0) throw new OWFileNotFoundException("Invalid path, no elements"); // check to see if this file entry has been found if (feStartPage <= 0) { // loop up to 2 times if getting 1-Wire IO exceptions do { try { if (verifyPath(path.size())) return; } catch (OneWireException e) { last_error = e.toString(); } } while (cnt++ < 2); // could not find file so pass along the last error throw new OWFileNotFoundException(last_error); } } } /** * Closes this file descriptor and releases any system resources * associated with this stream. Any cached writes are flushed into * the filesystem. This file descriptor may no longer * be used for writing bytes. If successfull (no exceptions) then * the following class member variables will be set: * <ul> * <li> fePage - File Entry page number * <li> feOffset - Offset into File Entry page * <li> feData - buffer containing the last File Entry Page * <li> feLen - length of packet in the last File Entry Page * <li> feNumPages - Number of Pages specified in File Entry * <li> feStartPage - Start Page specified in the File Entry * <li> feParentPage - Parent page of current File Entry Page * <li> feParentOffset - Offset into Parent page * <li> lastPage - (file only) last page read * <li> lastOffset - (file only) offset into last page read * <li> lastLen - (file only) length of last page read * <li> lastPageData - (file only) buffer for the last page read * <li> filePosition - (file only) overall file position when reading * </ul> * * @throws IOException if an I/O error occurs */ protected void close() throws IOException { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===close"); //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) { System.out.println("thread " + Thread.currentThread().hashCode()); Thread.dumpStack(); } // sync the cache to the device try { sync(); } catch (OWSyncFailedException e) { throw new IOException(e.toString()); } // free the resources for this fd free(); } } /** * Creates a directory or file to write. * * @param append for files only, true to append data to end of file, * false to reset the file * @param isDirectory true if creating a directory, false for a file * @param makeParents true if creating all needed parent directories * in order to create the file/directory * @param startPageNum starting page of file/directory, -1 if not * renaming * @param numberPages number of pages in file/directory, -1 if not * renaming * * @throws FileNotFoundException if file already opened to write, if * makeParents=false and parent directories not found, if * file is read only, or if there is an IO error reading * filesystem */ protected void create(boolean append, boolean isDirectory, boolean makeParents, int startPage, int numberPages) throws OWFileNotFoundException { byte[] element; boolean element_found; synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===create, appent=" + append + " isDirectory=" + isDirectory + " makeParents=" + makeParents); // clear last page read flag in the cache cache.clearLastPageRead(); // reset the file position lastPage = -1; // check if had an invalid path if (path == null) throw new OWFileNotFoundException("Invalid path"); // check if have an empty path if (path.size() == 0) throw new OWFileNotFoundException("Invalid path, no elements"); // make sure last element in path is a directory (or unknown) if making directory if (isDirectory || makeParents) { element = (byte[]) path.elementAt(path.size() - 1); if (((element [4] & 0x7F) != EXT_UNKNOWN) && ((element [4] & 0x7F) != EXT_DIRECTORY)) throw new OWFileNotFoundException( "Invalid path, directory has an extension"); } // check if file is already opened to write if (!isDirectory) { if (cache.isOpenedToWrite(owd[0].getAddressAsString() + getPath(), true)) throw new OWFileNotFoundException("File already opened to write"); openedToWrite = true; } // loop through the path elements, creating directories/file as needed feStartPage = 0; boolean file_exists = false; byte[] prev_element = { (byte)0x52, (byte)0x4F, (byte)0x4F, (byte)0x54 }; int prev_element_start = 0; for (int element_num = 0; element_num < path.size(); element_num++) { element = (byte[]) path.elementAt(element_num); try { element_found = findElement(feStartPage, element, 0); } catch (OneWireException e) { throw new OWFileNotFoundException(e.toString()); } if (!element_found) { if (isDirectory) { // convert unknown entry to directory if ((byte) element [4] == (byte) EXT_UNKNOWN) element [4] = (byte) EXT_DIRECTORY; if ((element_num != (path.size() - 1)) && !makeParents) throw new OWFileNotFoundException( "Invalid path, parent not found"); createEntry(element, startPage, numberPages, prev_element, prev_element_start); } else { // convert unknown entry to file with 0 extension if ((byte) element [4] == (byte) EXT_UNKNOWN) element [4] = 0; if (element_num == (path.size() - 1)) { // this is the file (end of path) createEntry(element, startPage, numberPages, prev_element, prev_element_start); } else { // remove the entry in the cache before throwing exception cache.removeWriteOpen(owd[0].getAddressAsString() + getPath()); throw new OWFileNotFoundException("Path not found"); } } } else if (element_num == (path.size() - 1)) { // last element if (isDirectory) { if (startPage != -1) throw new OWFileNotFoundException( "Destination File exists"); } else { // check if last element is a directory and should be a file if ((element[4] == (byte)EXT_DIRECTORY) && (!isDirectory)) { // remove the entry in the cache before throwing exception cache.removeWriteOpen(owd[0].getAddressAsString() + getPath()); throw new OWFileNotFoundException( "Filename provided is a directory!"); } file_exists = true; } } // get pointers to the next element feStartPage = Convert.toInt(feData, feOffset + LEN_FILENAME, LEN_PAGE_PTR); feNumPages = Convert.toInt(feData, feOffset + LEN_FILENAME + LEN_PAGE_PTR, LEN_PAGE_PTR); prev_element = element; prev_element_start = feStartPage; //\\//\\//\\//\\//\\//\\//\\//
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?