owfiledescriptor.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 1,991 行 · 第 1/5 页

JAVA
1,991
字号
/*--------------------------------------------------------------------------- * Copyright (C) 2001 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.application.file;import java.util.Hashtable;import java.util.Vector;import java.io.IOException;import java.lang.NumberFormatException;import com.dalsemi.onewire.adapter.DSPortAdapter;import com.dalsemi.onewire.OneWireException;import com.dalsemi.onewire.adapter.OneWireIOException;import com.dalsemi.onewire.container.OneWireContainer;import com.dalsemi.onewire.utils.Address;import com.dalsemi.onewire.utils.Convert;import com.dalsemi.onewire.utils.Bit;import com.dalsemi.onewire.container.PagedMemoryBank;/** * Instances of the 1-Wire file descriptor class serve as an opaque handle * to the underlying machine-specific structure representing an open * file, an open socket, or another source or sink of bytes. The * main practical use for a file descriptor is to create a * <code>OWFileInputStream</code> or <code>OWFileOutputStream</code> to * contain it. * <p> * Applications should not create their own file descriptors. * * @author  DS * @version 0.01, 1 June 2001 * @see     com.dalsemi.onewire.application.file.OWFile * @see     com.dalsemi.onewire.application.file.OWFileInputStream * @see     com.dalsemi.onewire.application.file.OWFileOutputStream */public class OWFileDescriptor{   //--------   //-------- Static Variables   //--------   /** Hashtable to contain MemoryCache instances (one per container) */   private static Hashtable memoryCacheHash = new Hashtable(4);   /** Field EXT_DIRECTORY entension value           */   private static final byte EXT_DIRECTORY = 0x007F;   /** Field EXT_UNKNOWN marker in path vector to indicate don't     * know if file or directory     */   private static final byte EXT_UNKNOWN = 0x007E;   /** Field BM_CACHE bitmap type MemoryCache    */   private static final int BM_CACHE = 0;   /** Field BM_LOCAL bitmap type Local (in directory page 0) */   private static final int BM_LOCAL = 1;   /** Field BM_FILE bitmap type file, in an external file */   private static final int BM_FILE = 2;   /** Field PAGE_USED marker for a used page in the bitmap */   private static final int PAGE_USED = 1;   /** Field PAGE_NOT_USED marker for an unused page in the bitmap */   private static final int PAGE_NOT_USED = 0;   /** Field LEN_FILENAME           */   private static final int LEN_FILENAME = 5;   /** Enable/disable debug messages */   private static final boolean doDebugMessages = false;   //--------   //-------- Variables   //--------   /** Field address - 1-Wire device address */   private Long address;   /** Field cache - used to read/write 1-Wire device */   private MemoryCache cache;   /** Field owd - is the 1-Wire container */   private OneWireContainer[] owd;   /** Field rawPath - what was provided in constructor except for toUpper */   private String rawPath;   /** Field path - converted path to vector of 5 byte arrays */   private Vector path;   /** Field verbosePath - same as 'path' but includes '.' and '..' */   private Vector verbosePath;   //--------   // file entry (fe) info on device   //--------   /** Field fePage - File Entry page number */   private int fePage;   /** Field feOffset - Offset into File Entry page */   private int feOffset;   /** Field feData - buffer containing the last File Entry Page */   private byte[] feData;   /** Field feLen - length of packet in the last File Entry Page */   private int feLen;   /** Field feNumPages - Number of Pages specified in File Entry */   private int feNumPages;   /** Field feStartPage - Start Page specified in the File Entry */   private int feStartPage;   /** Field feParentPage - Parent page of current File Entry Page */   private int feParentPage;   /** Field feParentOffset - Offset into Parent page */   private int feParentOffset;   //--------   // file read/write info   //--------   /** Field lastPage - last page read */   private int lastPage;   /** Field lastOffset - offset into last page read */   private int lastOffset;   /** Field lastLen - length of last page read */   private int lastLen;   /** Field lastPageData - buffer for the last page read */   private byte[] lastPageData;   /** Field filePosition - overall file position when reading */   private int filePosition;   /** Field markPosition - mark position in read file */   private int markPosition;   /** Field markLimit - mark position limit */   private int markLimit;   //--------   // total device info   //--------   /** Field totalPages - number of pages in filesystem */   private int totalPages;   /** Field rootTotalPages - number of pages on the ROOT device in the filesystem */   private int rootTotalPages;   /** Field maxDataLen - max data per page including page pointer */   private int maxDataLen;   /** Field LEN_PAGE_PTR - length in bytes for the page pointer */   private int LEN_PAGE_PTR;   /** Field LEN_FILE_ENTRY - length in bytes of the directory file entry */   private int LEN_FILE_ENTRY;   /** Field LEN_FILE_ENTRY - length in bytes of the directory control Data */   private int LEN_CONTROL_DATA;   /** Field openedToWrite - flag to indicate file is opened for writing */   private boolean openedToWrite;   //--------   // page used bitmap   //--------   /** Field lastFreePage - last free page */   private int lastFreePage;   /** Field bitmapType - type of page bitmap */   private int bitmapType;   /** Field pbm - buffer containering the current image for the page bitmap */   private byte[] pbm;   /** Field pbmByteOffset - byte offset into page bitmap */   private int pbmByteOffset;   /** Field pbmBitOffset - bit offset into page bitmap */   private int pbmBitOffset;   /** Field pbmStartPage - start page of page bitmap */   private int pbmStartPage;   /** Field pbmNumPages - number of pages in the page bitmap */   private int pbmNumPages;   //--------   // Misc   //--------   /** Field tempPage - temporary page buffer */   private byte[] tempPage;   /** Field initName - image of blank directory entry, used in parsing */   private byte[] initName = {0x20, 0x20, 0x20, 0x20, EXT_UNKNOWN};   /** Field smallBuf - small buffer */   private byte[] smallBuf;   /** Field dmBuf - device map page buffer */   private byte[] dmBuf;   /** Field addrBuf - address buffer */   private byte[] addrBuf;   //--------   //-------- Constructors   //--------   /**    * Construct an invalid 1-Wire FileDescriptor    *    */   public OWFileDescriptor()   {      //\\//\\//\\//\\//\\//\\//\\//      if (doDebugMessages)         System.out.println("===Invalid Constructor OWFileDescriptor ");   }   /**    * Construct a 1-Wire FileDescrioptor providing the Filesystem    * 1-Wire device and file path.    *    * @param owd - 1-Wire container where the filesystem resides    * @param newPath - path containing the file/directory that represents    *                  this file descriptor    */   protected OWFileDescriptor(OneWireContainer owd, String newPath)   {      OneWireContainer[] devices = new OneWireContainer[1];      devices[0] = owd;      //\\//\\//\\//\\//\\//\\//\\//      if (doDebugMessages)         System.out.println("===Constructor OWFileDescriptor with device: "                            + devices[0].getAddressAsString() + " and path: "                            + newPath);      setupFD(devices,newPath);   }   /**    * Construct a 1-Wire FileDescrioptor providing the Filesystem    * 1-Wire device and file path.    *    * @param owd - 1-Wire container where the filesystem resides    * @param newPath - path containing the file/directory that represents    *                  this file descriptor    */   protected OWFileDescriptor(OneWireContainer[] owd, String newPath)   {      //\\//\\//\\//\\//\\//\\//\\//      if (doDebugMessages)         System.out.println("===Constructor OWFileDescriptor with device: "                            + owd[0].getAddressAsString() + " and path: "                            + newPath);      setupFD(owd,newPath);   }   /**    * Setups the 1-Wire FileDescrioptor providing the Filesystem    * 1-Wire device(s) and file path.    *    * @param owd - 1-Wire container where the filesystem resides    * @param newPath - path containing the file/directory that represents    *                  this file descriptor    */   protected void setupFD(OneWireContainer[] owd, String newPath)   {      // synchronize with the static memoryCacheHash while initializing      synchronized (memoryCacheHash)      {         // keep reference to container, adapter, and name         this.owd = owd;         if (newPath != null)            this.rawPath = newPath.toUpperCase();         else            this.rawPath = "";         // check the hash to see if already have a MemoryCache for this device         address = new Long(owd[0].getAddressAsLong());         cache   = (MemoryCache) memoryCacheHash.get(address);         if (cache == null)         {            // create a new cache            cache = new MemoryCache(owd);            // add to hash            memoryCacheHash.put(address, cache);         }         // indicate this fd uses this cache, used later in cleanup         cache.addOwner(this);         // get info on device through cache         totalPages     = cache.getNumberPages();         rootTotalPages = cache.getNumberPagesInBank(0);         maxDataLen     = cache.getMaxPacketDataLength();         openedToWrite  = false;         //\\//\\//\\//\\//\\//\\//\\//         if (doDebugMessages)            System.out.println("=cache has totalPages = " + totalPages                            + " with max data " + maxDataLen);         // construct the page bufs         lastPageData = new byte [maxDataLen];         tempPage     = new byte [lastPageData.length];         feData       = new byte [lastPageData.length];         dmBuf        = new byte [lastPageData.length];         smallBuf     = new byte [10];         addrBuf      = new byte [8];         // guese at the number of bytes to represent a page number         // since have not read the root directory yet this may change         LEN_PAGE_PTR = (totalPages > 256) ? 2                                      : 1;         LEN_FILE_ENTRY = LEN_FILENAME + LEN_PAGE_PTR * 2;         LEN_CONTROL_DATA = 6 + LEN_PAGE_PTR;         //\\//\\//\\//\\//\\//\\//\\//         if (doDebugMessages)            System.out.println("=Number of page bytes = " + LEN_PAGE_PTR                               + " with directory entry size of " + LEN_FILE_ENTRY);         // decide what type of bitmap we will have         if (cache.handlePageBitmap())            bitmapType = BM_CACHE;         else if (totalPages <= 32)         {            bitmapType = BM_LOCAL;            // make PageBitMap max size of first page of directory            pbm           = new byte [maxDataLen];            pbmByteOffset = 3;            pbmBitOffset  = 0;         }         else         {            bitmapType = BM_FILE;            // make PageBitMap correct size number of pages in fs            pbm           = new byte [totalPages / 8 + LEN_PAGE_PTR];            pbmByteOffset = 0;            pbmBitOffset  = 0;         }         pbmStartPage = -1;         //\\//\\//\\//\\//\\//\\//\\//         if (doDebugMessages)            System.out.println("=Page BitMap type is " + bitmapType                               + " with bit offset of " + pbmBitOffset);         // parse the path into a Vector         verbosePath = new Vector(3);         parsePath(rawPath, verbosePath);         // done could not parse the skip compressing         if (verbosePath == null)            return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?