owfiledescriptor.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 1,991 行 · 第 1/5 页
JAVA
1,991 行
} /** * Checks to see if can read the file associated with * this descriptor. * * @return true if this file exists, false otherwise */ protected boolean canRead() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===canRead()"); return exists(); } } /** * Checks to see if the file represented by this descriptor * is writable. * * @return true if this file exists and is not read only, false * otherwise */ protected boolean canWrite() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===canWrite()"); if (exists()) { if (isFile()) return ((feData [feOffset + LEN_FILENAME - 1] & 0x80) == 0); else return true; } else return false; } } /** * Checks to see if this is a directory. * * @return true if this file exists and it is a directory, false * otherwise */ protected boolean isDirectory() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===isDirectory()"); if (exists()) return !isFile(); else return false; } } /** * Checks to see if this is a file * * @return true if this file exists and is a file, false * otherwise */ protected boolean isFile() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===isFile()"); // check if this is the root if (path.size() == 0) return false; if (exists()) return ((feData [feOffset + LEN_FILENAME - 1] & 0x7F) != 0x7F); else return false; } } /** * Checks to see if this directory is hidden. * * @return true if this is a directory and is marked as hidden, false * otherwise */ protected boolean isHidden() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===isHidden()"); if (exists()) { // look at hidden flag in parent (if it has one) if (path.size() > 0) { if (isDirectory()) { byte[] fl = (byte[]) path.elementAt(path.size() - 1); return ((fl [LEN_FILENAME - 1] & 0x80) != 0); } } } return false; } } /** * Get the estimated length of the file represented by this * descriptor. This is calculated by looking at how may pages * the file is using so is not a very accurate measure. * * @return estimated length of file in bytes */ protected long length() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===length()"); if (exists()) return (feNumPages * (maxDataLen - LEN_PAGE_PTR)); else return 0; } } /** * Delete this file or directory represented by this descriptor. * Will fail if it is a read-only file or a non-empty directory. * * @return true if the file/directory was successfully deleted or * false if not */ protected boolean delete() { synchronized (cache) { // clear last page read flag in the cache cache.clearLastPageRead(); if (isFile()) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===delete() is a file"); try { // remove the directory entry System.arraycopy(feData, feOffset + LEN_FILE_ENTRY, feData, feOffset, feLen - feOffset - LEN_FILE_ENTRY); feLen -= LEN_FILE_ENTRY; cache.writePagePacket(fePage, feData, 0, feLen); // loop to remove all of the file pages 'free' only if not EPROM if (bitmapType != BM_CACHE) { // loop to read the rest of the pages and 'free' them int next_page = feStartPage; while (next_page != 0) { // free the page readBitMap(); freePage(next_page); writeBitMap(); // read the file page lastLen = cache.readPagePacket(next_page, lastPageData, 0); // get the next page pointer next_page = Convert.toInt(lastPageData, lastLen - LEN_PAGE_PTR, LEN_PAGE_PTR); } // update lastPage = -1; feStartPage = -1; } return true; } catch (OneWireException e) { return false; } } else if (isDirectory()) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===delete() is a directory"); try { // read the first page of the directory to see if empty int len = cache.readPagePacket(feStartPage, tempPage, 0); if (len != LEN_FILE_ENTRY + LEN_PAGE_PTR) return false; // remove the directory entry System.arraycopy(feData, feOffset + LEN_FILE_ENTRY, feData, feOffset, feLen - feOffset - LEN_FILE_ENTRY); feLen -= LEN_FILE_ENTRY; cache.writePagePacket(fePage, feData, 0, feLen); // free the page readBitMap(); freePage(feStartPage); writeBitMap(); return true; } catch (OneWireException e) { return false; } } else { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println( "===delete() is neither file or directory so fail"); return false; } } } /** * Returns an array of strings naming the files and directories in the * directory denoted by this abstract pathname. * * <p> If this abstract pathname does not denote a directory, then this * method returns <code>null</code>. Otherwise an array of strings is * returned, one for each file or directory in the directory. Names * denoting the directory itself and the directory's parent directory are * not included in the result. Each string is a file name rather than a * complete path. * * <p> There is no guarantee that the name strings in the resulting array * will appear in any specific order; they are not, in particular, * guaranteed to appear in alphabetical order. * * @return An array of strings naming the files and directories in the * directory denoted by this abstract pathname. The array will be * empty if the directory is empty. Returns <code>null</code> if * this abstract pathname does not denote a directory, or if an * I/O error occurs. */ protected String[] list() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===list() string"); // clear last page read flag in the cache cache.clearLastPageRead(); if (isDirectory()) { Vector entries = new Vector(1); try { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("=feStartPage " + feStartPage); // loop though the entries and collect string reps int next_page = feStartPage; StringBuffer build_buffer = new StringBuffer(); int offset = LEN_CONTROL_DATA, len, i, page; do { page = next_page; // read the page len = cache.readPagePacket(page, tempPage, 0); // loop through the entries for (; offset < (len - LEN_PAGE_PTR); offset += LEN_FILE_ENTRY) { build_buffer.setLength(0); for (i = 0; i < 4; i++) { if ((byte) tempPage [offset + i] != (byte) 0x20) build_buffer.append((char) tempPage [offset + i]); else break; } if ((byte) (tempPage [offset + 4] & 0x7F) != (byte) EXT_DIRECTORY) build_buffer.append( "." + Integer.toString( (int) (tempPage [offset + 4] & 0x7F))); //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("=entry= " + build_buffer.toString()); // only add if not hidden directory if ((byte) tempPage [offset + 4] != (byte) 0xFF ) // add to the vector of strings entries.addElement(build_buffer.toString()); } // get next page pointer to read next_page = Convert.toInt(tempPage, len - LEN_PAGE_PTR, LEN_PAGE_PTR); offset = 0; // check for looping Filesystem if (entries.size() > totalPages) return null; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("=next page = " + next_page); } while (next_page != 0); } catch (OneWireException e) { // DRAIN //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("= " + e); } // return the entries as an array of strings String[] strs = new String [entries.size()]; for (int i = 0; i < strs.length; i++) strs[i] = (String)entries.elementAt(i); return strs; } else { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("=not a directory so no list"); return null; } } } /** * Renames the file denoted by this abstract pathname. * * @param dest The new abstract
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?