owfiledescriptor.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 1,991 行 · 第 1/5 页
JAVA
1,991 行
// get the data from the page (if buffer not null) if (b != null) System.arraycopy(lastPageData, lastOffset, b, off, page_data_read); // adjust counters read_count += page_data_read; off += page_data_read; len -= page_data_read; lastOffset += page_data_read; filePosition += page_data_read; next_page = Convert.toInt(lastPageData, lastLen - LEN_PAGE_PTR, LEN_PAGE_PTR); } while ((len != 0) && (next_page != 0)); // check for end of file if ((read_count == 0) && (len != 0)) return -1; // return number of bytes read return read_count; } } /** * Reads a byte of data from this input stream. This method blocks * if no input is yet available. * * @return the next byte of data, or <code>-1</code> if the end of the * file is reached. * @exception IOException if an I/O error occurs. */ protected int read() throws IOException { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===read()"); int len = read(smallBuf, 0, 1); if (len == 1) return (int) (smallBuf [0] & 0x00FF); else return -1; } } /** * Skips over and discards <code>n</code> bytes of data from the * input stream. The <code>skip</code> method may, for a variety of * reasons, end up skipping over some smaller number of bytes, * possibly <code>0</code>. The actual number of bytes skipped is returned. * * @param n the number of bytes to be skipped. * @return the actual number of bytes skipped. * @exception IOException if an I/O error occurs. */ protected long skip(long n) throws IOException { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===skip " + n); return read(null, 0, (int) n); } } /** * Returns the number of bytes that can be read from this file input * stream without blocking. * * @return the number of bytes that can be read from this file input * stream without blocking. * @exception IOException if an I/O error occurs. */ protected int available() throws IOException { synchronized (cache) { // check for no pages read if (lastPage == -1) return 0; else return (lastLen - lastOffset - 1); } } //-------- //-------- Write methods //-------- /** * Writes the specified byte to this file output stream. Implements * the <code>write</code> method of <code>OutputStream</code>. * * @param b the byte to be written. * @exception IOException if an I/O error occurs. */ protected void write(int b) throws IOException { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===write(int) " + b); smallBuf [0] = (byte) b; write(smallBuf, 0, 1); } } /** * Writes <code>len</code> bytes from the specified byte array * starting at offset <code>off</code> to this file output stream. * * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. * @exception IOException if an I/O error occurs. */ protected void write(byte b [], int off, int len) throws IOException { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) { System.out.print("===write(byte[],int,int) with data (" + len + ") :"); debugDump(b, off, len); } // check for something to do if (len == 0) return; // clear last page read flag in the cache cache.clearLastPageRead(); // check for no pages read if (lastPage == -1) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("=first write"); lastPage = feStartPage; lastOffset = 0; filePosition = 0; } try { // read the last page lastLen = cache.readPagePacket(lastPage, lastPageData, 0); //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===write, readpagePacket " + lastPage + " got len " + lastLen); int write_len; do { // check if room to write if (lastLen >= maxDataLen) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.print("=Need new page"); // get another page to write // get the pagebitmap readBitMap(); // get the next available page int new_page = getFirstFreePage(false); // verify got a free page if (new_page < 0) { try { sync(); } catch (OWSyncFailedException e) { // DRAIN } throw new IOException("Out of space on 1-Wire device"); } // mark page used markPageUsed(new_page); // put blank data page in new page Convert.toByteArray(0, tempPage, 0, LEN_PAGE_PTR); cache.writePagePacket(new_page, tempPage, 0, LEN_PAGE_PTR); // change next page pointer in last page Convert.toByteArray(new_page, lastPageData, lastLen - LEN_PAGE_PTR, LEN_PAGE_PTR); // put data page back in place with new next page pointer cache.writePagePacket(lastPage, lastPageData, 0, lastLen); // write the page bitmap writeBitMap(); // update the directory entry to include this new page lastLen = cache.readPagePacket(fePage, lastPageData, 0); Convert.toByteArray(++feNumPages, lastPageData, feOffset + LEN_FILENAME + LEN_PAGE_PTR, LEN_PAGE_PTR); cache.writePagePacket(fePage, lastPageData, 0, lastLen); // make 'lastPage' the new empty page lastPageData [0] = 0; lastPage = new_page; lastLen = LEN_PAGE_PTR; } // calculate how much of the data can write to lastPage if (len > (maxDataLen - lastLen)) write_len = maxDataLen - lastLen; else write_len = len; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===write, len " + len + " maxDataLen " + maxDataLen + " lastLen " + lastLen + " write_len " + write_len + " off " + off); // copy the data System.arraycopy(b, off, lastPageData, lastLen - LEN_PAGE_PTR, write_len); // update the counters len -= write_len; off += write_len; lastLen += write_len; // set the next page pointer to end of file marker '0' Convert.toByteArray(0, lastPageData, lastLen - LEN_PAGE_PTR, LEN_PAGE_PTR); // write the data cache.writePagePacket(lastPage, lastPageData, 0, lastLen); } while (len > 0); } catch (OneWireException e) { throw new IOException(e.toString()); } } } //-------- //-------- Info methods //-------- /** * Returns the name of the file or directory denoted by this abstract * pathname. This is just the last name in the pathname's name * sequence. If the pathname's name sequence is empty, then the empty * string is returned. * * @return The name of the file or directory denoted by this abstract * pathname, or the empty string if this pathname's name sequence * is empty */ protected String getName() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===getName()"); return pathToString(path, path.size() - 1, path.size(), true); } } /** * Returns the pathname string of this abstract pathname's parent, or * <code>null</code> if this pathname does not name a parent directory. * * <p> The <em>parent</em> of an abstract pathname consists of the * pathname's prefix, if any, and each name in the pathname's name * sequence except for the last. If the name sequence is empty then * the pathname does not name a parent directory. * * @return The pathname string of the parent directory named by this * abstract pathname, or <code>null</code> if this pathname * does not name a parent */ protected String getParent() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===getParent(), path is null=" + (path == null)); if (path == null) throw new NullPointerException("path is not valid"); if (path.size() >= 1) return pathToString(path, 0, path.size() - 1, false); else return null; } } /** * Converts this abstract pathname into a pathname string. The resulting * string uses the {@link OWFile#separator default name-separator character} to * separate the names in the name sequence. * * @return The string form of this abstract pathname */ protected String getPath() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===getPath(), path is null=" + (path == null)); if (path == null) throw new NullPointerException("path is not valid"); return pathToString(verbosePath, 0, verbosePath.size(), false); } } /** * Checks to see if the file exists * * @return true if the file exists and false otherwise */ protected boolean exists() { synchronized (cache) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("===exists()"); // clear last page read flag in the cache cache.clearLastPageRead(); // check if this is the root if (path.size() == 0) { // force a check of the Filesystem if never been read if (pbmStartPage == -1) { try { readBitMap(); } catch (OneWireException e) { return false; } } return true; } // attempt to open the file/directory try { open(); return true; } catch (OWFileNotFoundException e) { return false; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?