owfiledescriptor.java

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

JAVA
1,991
字号
            if (doDebugMessages)               System.out.println("=feStartPage " + feStartPage + " feNumPages "                                  + feNumPages);         }         // if is a file and it already exists, free all but the first data page         if (file_exists)         {            //\\//\\//\\//\\//\\//\\//\\//            if (doDebugMessages)               System.out.println("=file exists");            // check for readonly            if (!canWrite())               throw new OWFileNotFoundException(                  "File is read only (access is denied)");            try            {               // read the first file page               lastLen = cache.readPagePacket(feStartPage, lastPageData, 0);               // write over this with an 'empty' page               Convert.toByteArray(0, smallBuf, 0, LEN_PAGE_PTR);               cache.writePagePacket(feStartPage, smallBuf, 0, LEN_PAGE_PTR);               // loop to read the rest of the pages and 'free' them               int next_page = Convert.toInt(lastPageData,                                             lastLen - LEN_PAGE_PTR, LEN_PAGE_PTR);               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 the directory entry to free the pages               feNumPages = 1;               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);               // set the lastPage pointer to the current page               lastPage = feStartPage;            }            catch (OneWireException e)            {               throw new OWFileNotFoundException(e.toString());            }         }      }   }   /**    * Format the Filesystem on the 1-Wire device.    * <p>WARNING: all files/directories will be deleted in the process.    *    * @throws OneWireException when adapter is not setup properly    * @throws OneWireIOException when an IO error occured reading    *         the 1-Wire device    */   protected void format()      throws OneWireException, OneWireIOException   {      int i, j, len, next_page, cnt, cdcnt = 0, device_map_pages, dm_bytes = 0;      synchronized (cache)      {         // clear last page read flag in the cache         cache.clearLastPageRead();         // check for device with no memory         if (totalPages == 0)            throw new OneWireException("1-Wire Filesystem does not have memory");         for (i = 0; i < feData.length; i++)            feData [i] = 0;         // create the directory page         // Directory Marker 'DM'         feData[cdcnt] = (LEN_PAGE_PTR == 1) ? (byte) 0x0A : (byte) 0x0B;         feData[cdcnt++] |= (owd.length == 1) ? (byte)0xA0 : 0xB0;         // Map Address 'MA', skip for now         cdcnt += LEN_PAGE_PTR;         // decide what type of bitmap we will have         if (cache.handlePageBitmap())         {            bitmapType = BM_CACHE;            feData [cdcnt++] = 0;            Convert.toByteArray(cache.getBitMapPageNumber(),                                feData, cdcnt, LEN_PAGE_PTR);            cdcnt += LEN_PAGE_PTR;            Convert.toByteArray(cache.getBitMapNumberOfPages(),                                feData, cdcnt, LEN_PAGE_PTR);         }         // regular bitmap         else         {            // check for Device Map file            if (owd.length > 1)            {               // calculate the number of pages need so leave space               dm_bytes = (owd.length - 1) * 8;               device_map_pages = dm_bytes / (maxDataLen - LEN_PAGE_PTR);               if ((dm_bytes % (maxDataLen - LEN_PAGE_PTR)) > 0)                  device_map_pages++;            }            else               device_map_pages = 0;            // local            if (totalPages <= 32)            {               bitmapType = BM_LOCAL;               // make PageBitMap max size of first page of directory               pbm           = new byte [maxDataLen];               pbmByteOffset = 3;               pbmBitOffset  = 0;               // 'BC'               feData [cdcnt++] = (owd.length > 1) ? (byte) 0x82 : (byte) 0x80;               // check if this will fit on the ROOT device               if (device_map_pages >= rootTotalPages)                  throw new OneWireException("ROOT 1-Wire device does not have memory to support this many SATELLITE devices");               // set local page bitmap               for (i = 0; i <= device_map_pages; i++)                  Bit.arrayWriteBit(PAGE_USED, i, cdcnt, feData);               // put dummy directory on each SATELLITE device               if (owd.length > 1)               {                  // create the dummy directory                  tempPage[0] = feData[0];                  tempPage[LEN_PAGE_PTR] = 0;                  tempPage[1] = (byte)0x01;                  tempPage[LEN_PAGE_PTR + 1] = (byte)0x80;                  for (j = 2; j <= 5; j++)                     tempPage[LEN_PAGE_PTR + j] = (byte)0xFF;                  for (j = 6; j <= 7; j++)                     tempPage[LEN_PAGE_PTR + j] = (byte)0x00;                  // create link back to the MASTER                  System.arraycopy(owd[0].getAddress(), 0, smallBuf, 0, 8);                  smallBuf[8] = 0;                  smallBuf[9] = 0;                  // write dummy directory on each SATELLITE device and mark in bitmap                  for (i = 1; i < owd.length; i++)                  {                     // dummy directory                     cache.writePagePacket(cache.getPageOffsetForDevice(i), tempPage, 0, LEN_PAGE_PTR * 2 + 6);                     Bit.arrayWriteBit(PAGE_USED, cache.getPageOffsetForDevice(i), cdcnt, feData);                     // MASTER device map link                     cache.writePagePacket(cache.getPageOffsetForDevice(i) + 1, smallBuf, 0, LEN_PAGE_PTR + 8);                     Bit.arrayWriteBit(PAGE_USED, cache.getPageOffsetForDevice(i) + 1, cdcnt, feData);                  }               }            }            // file            else            {               bitmapType    = BM_FILE;               pbmByteOffset = 0;               pbmBitOffset  = 0;               // calculate the number of bitmap pages needed               int pbm_bytes = (totalPages / 8);               int pgs       = pbm_bytes / (maxDataLen - LEN_PAGE_PTR);               if ((pbm_bytes % (maxDataLen - LEN_PAGE_PTR)) > 0)                  pgs++;               // check if this will fit on the ROOT device               if ((device_map_pages + pgs) >= rootTotalPages)                  throw new OneWireException("ROOT 1-Wire device does not have memory to support this many SATELLITE devices");               // 'BC' set the page number of the bitmap file               feData [cdcnt++] = (owd.length > 1) ? (byte) 0x02 : (byte) 0x00;               // page address and number of pages for bitmap file               if (LEN_PAGE_PTR == 1)               {                  feData[cdcnt++] = 0;                  feData[cdcnt++] = 0;               }               Convert.toByteArray(device_map_pages + 1,                                   feData, cdcnt, LEN_PAGE_PTR);               cdcnt += LEN_PAGE_PTR;               Convert.toByteArray(pgs, feData, cdcnt, LEN_PAGE_PTR);               // clear the bitmap               for (i = 0; i < pbm.length; i++)                  pbm [i] = 0;               // set the pages used by the directory and bitmap file and device map               for (i = 0; i <= (pgs + device_map_pages); i++)                  Bit.arrayWriteBit(PAGE_USED, pbmBitOffset + i, pbmByteOffset, pbm);               // put dummy directory on each SATELLITE device               if (owd.length > 1)               {                  // create the dummy directory                  tempPage[0] = feData[0];                  tempPage[LEN_PAGE_PTR] = 0;                  tempPage[1] = (byte)0x01;                  tempPage[LEN_PAGE_PTR + 1] = (byte)0x80;                  for (j = 2; j <= 5; j++)                     tempPage[LEN_PAGE_PTR + j] = (byte)0xFF;                  for (j = 6; j <= 7; j++)                     tempPage[LEN_PAGE_PTR + j] = (byte)0x00;                  // create link back to the MASTER                  System.arraycopy(owd[0].getAddress(), 0, smallBuf, 0, 8);                  smallBuf[8] = 0;                  smallBuf[9] = 0;                  // write dummy directory on each SATELLITE device and mark in bitmap                  for (i = 1; i < owd.length; i++)                  {                     // dummy directory                     cache.writePagePacket(cache.getPageOffsetForDevice(i), tempPage, 0, LEN_PAGE_PTR * 2 + 6);                     Bit.arrayWriteBit(PAGE_USED, pbmBitOffset + cache.getPageOffsetForDevice(i), pbmByteOffset, pbm);                     // MASTER device map link                     cache.writePagePacket(cache.getPageOffsetForDevice(i) + 1, smallBuf, 0, LEN_PAGE_PTR + 8);                     Bit.arrayWriteBit(PAGE_USED, pbmBitOffset + cache.getPageOffsetForDevice(i) + 1, pbmByteOffset, pbm);                  }               }               // write the bitmap file               cnt = 0;               for (i = device_map_pages + 1; i <= (pgs + device_map_pages); i++)               {                  // calculate length to write for this page                  if ((pbm_bytes - cnt) > (maxDataLen - LEN_PAGE_PTR))                     len = maxDataLen - LEN_PAGE_PTR;                  else                     len = pbm_bytes - cnt;                  // copy bitmap data to temp                  System.arraycopy(pbm, pbmByteOffset + cnt, tempPage, 0, len);                  // set the next page marker                  next_page = (i == (pgs + device_map_pages)) ? 0 : (i + 1);                  Convert.toByteArray(next_page, tempPage, len, LEN_PAGE_PTR);                  // write the page                  cache.writePagePacket(i, tempPage, 0, len + LEN_PAGE_PTR);                  cnt += len;               }            }            // write Device Map file            if (owd.length > 1)            {               // set the start page 'MA'               Convert.toByteArray(1, feData, 1, LEN_PAGE_PTR);               // bitmap already taken care of, just put right after directory               // create the device map data to write               byte[] dmf = new byte[dm_bytes];               for (i = 1; i < owd.length; i++)                  System.arraycopy(owd[i].getAddress(), 0, dmf, (i - 1) * 8, 8);               // write the pages               cnt = 0;               for (i = 1; i <= device_map_pages; i++)               {                  // calculate length to write for this page                  if ((dm_bytes - cnt) > (maxDataLen - LEN_PAGE_PTR))                     len = maxDataLen - LEN_PAGE_PTR;                  else                     len = dm_bytes - cnt;                  // copy bitmap data to temp                  System.arraycopy(dmf, cnt, tempPage, 0, len);                  // set the next page marker                  next_page = (i == device_map_pages) ? 0 : (i + 1);                  Convert.toByteArray(next_page, tempPage, len, LEN_PAGE_PTR);                  // write the page                  cache.writePagePacket(i, tempPage, 0, len + LEN_PAGE_PTR);                  cnt += len;               }            }         }         // write the directory page         cache.writePagePacket(0, feData, 0, LEN_CONTROL_DATA + LEN_PAGE_PTR);         // update bitmap if implemented in cache         if (cache.handlePageBitmap())            markPageUsed(0);         fePage = 0;         feLen  = LEN_CONTROL_DATA + LEN_PAGE_PTR;      }   }   //--------   //-------- Read methods   //--------   /**    * Reads up to <code>len</code> bytes of data from this input stream    * into an array of bytes. This method blocks until some input is    * available.    *    * @param      b     the buffer into which the data is read.    * @param      off   the start offset of the data.    * @param      len   the maximum number of bytes read.    * @return     the total number of bytes read into the buffer, or    *             <code>-1</code> if there is no more data because the end of    *             the file has been reached.    * @exception  IOException  if an I/O error occurs.    */   protected int read(byte b [], int off, int len)      throws IOException   {      int read_count = 0, page_data_read, next_page = 0;      synchronized (cache)      {         //\\//\\//\\//\\//\\//\\//\\//         if (doDebugMessages)            System.out.println("===read(byte[],int,int)  with len " + len);         // clear last page read flag in the cache         cache.clearLastPageRead();         // check for no pages read         if (lastPage == -1)         {            //\\//\\//\\//\\//\\//\\//\\//            if (doDebugMessages)               System.out.println("=first read");            lastPage     = feStartPage;            lastOffset   = 0;            filePosition = 0;            // read the lastPage into the lastPageData buffer            fetchPage();         }         // loop to read pages needed or end of file found         do         {            // check if need to fetch another page            if ((lastOffset + LEN_PAGE_PTR) >= lastLen)            {               // any more pages?               next_page = Convert.toInt(lastPageData, lastLen - LEN_PAGE_PTR,                                         LEN_PAGE_PTR);               if (next_page == 0)                  break;               // get the next page               lastPage = next_page;               fetchPage();            }            // calculate the data available/needed to read from this page            if (len >= (lastLen - lastOffset - LEN_PAGE_PTR))               page_data_read = (lastLen - lastOffset - LEN_PAGE_PTR);            else               page_data_read = len;

⌨️ 快捷键说明

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