inode.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,246 行 · 第 1/3 页

JAVA
1,246
字号
	}	int fragOffset = (int) (currentLength % INODE_BLOCK_SIZE);	int sublen = length;	if (INODE_BLOCK_SIZE - fragOffset < sublen)	  sublen = INODE_BLOCK_SIZE - fragOffset;	store.writeFragment(xa, fragAddr, fragOffset, buffer, offset, sublen);	offset += sublen;	length -= sublen;	currentLength += sublen;      }      else {	int sublen = length;	if (FRAGMENT_SIZE < sublen)	  sublen = FRAGMENT_SIZE;	long fragAddr = store.allocateFragment(xa);	if (fragAddr == 0) {	  store.setCorrupted(true);	  	  throw new IllegalStateException(L.l("{0} illegal fragment",					      store));	}	writeFragmentAddr(inode, inodeOffset,			  store, xa,			  currentLength, fragAddr);	store.writeFragment(xa, fragAddr, 0, buffer, offset, sublen);		offset += sublen;	length -= sublen;	currentLength += sublen;      }    }  }  private static void appendBlock(byte []inode, int inodeOffset,				  Store store, StoreTransaction xa,				  byte []buffer, int offset, int length,				  long currentLength)    throws IOException  {    // XXX: theoretically deal with case of appending to inline, although    // the blobs are the only writers and will avoid that case.    while (length > 0) {      if ((currentLength - FRAGMENT_MAX) % BLOCK_SIZE != 0) {	long addr = readBlockAddr(inode, inodeOffset,				  store,				  currentLength);	if (addr == 0) {	  store.setCorrupted(true);	  	  throw new IllegalStateException(store + " inode: illegal block at " + currentLength);	}	int blockOffset = (int) ((currentLength - FRAGMENT_MAX) % BLOCK_SIZE);	int sublen = length;	if (BLOCK_SIZE - blockOffset < sublen)	  sublen = BLOCK_SIZE - blockOffset;	store.writeBlock(xa, addr, blockOffset, buffer, offset, sublen);	offset += sublen;	length -= sublen;	currentLength += sublen;      }      else {	int sublen = length;	if (BLOCK_SIZE < sublen)	  sublen = BLOCK_SIZE;	long blockAddr = store.allocateFragment(xa);	if (blockAddr == 0) {	  store.setCorrupted(true);	  	  throw new IllegalStateException(L.l("{0}: illegal fragment",					      store));	}	writeBlockAddr(inode, inodeOffset,		       store, xa,		       currentLength, blockAddr);	store.writeBlock(xa, blockAddr, 0, buffer, offset, sublen);		offset += sublen;	length -= sublen;	currentLength += sublen;      }    }  }  /**   * Reads into a buffer.   *   * @param inode the inode buffer   * @param inodeOffset the offset of the inode data in the buffer   * @param store the owning store   * @param fileOffset the offset into the file to read   * @param buffer the buffer receiving the data   * @param bufferOffset the offset into the receiving buffer   * @param bufferLength the maximum number of chars to read   *   * @return the number of characters read   */  static int read(byte []inode, int inodeOffset, Store store,		  long fileOffset, 		  char []buffer, int bufferOffset, int bufferLength)    throws IOException  {    long fileLength = readLong(inode, inodeOffset);    int sublen = (int) (fileLength - fileOffset) / 2;    if (bufferLength < sublen)      sublen = bufferLength;          if (sublen <= 0)      return -1;    if (fileLength <= INLINE_MAX) {      int baseOffset = inodeOffset + 8 + (int) fileOffset;      for (int i = 0; i < sublen; i++) {	char ch = (char) (((inode[baseOffset] & 0xff) << 8) +			  ((inode[baseOffset + 1] & 0xff)));	buffer[bufferOffset + i] = ch;	baseOffset += 2;      }      return sublen;    }    else if (fileLength <= MINI_FRAG_MAX) {      long fragAddr = readMiniFragAddr(inode, inodeOffset, store, fileOffset);      int fragOffset = (int) (fileOffset % Inode.MINI_FRAG_SIZE);      if (MINI_FRAG_SIZE - fragOffset < 2 * sublen)	sublen = (MINI_FRAG_SIZE - fragOffset) / 2;      store.readMiniFragment(fragAddr, fragOffset,			     buffer, bufferOffset, sublen);      return sublen;    }    else if (fileLength <= FRAGMENT_MAX) {      long fragAddr = readFragmentAddr(inode, inodeOffset, store, fileOffset);      int fragOffset = (int) (fileOffset % Inode.INODE_BLOCK_SIZE);      if (FRAGMENT_SIZE - fragOffset < 2 * sublen)	sublen = (FRAGMENT_SIZE - fragOffset) / 2;      store.readFragment(fragAddr, fragOffset, buffer, bufferOffset, sublen);          return sublen;    }    else {      long addr = readBlockAddr(inode, inodeOffset, store, fileOffset);      int offset = (int) ((fileOffset - FRAGMENT_MAX) % BLOCK_SIZE);      if (BLOCK_SIZE - offset < sublen)	sublen = BLOCK_SIZE - offset;      store.readBlock(addr, offset, buffer, bufferOffset, sublen);          return sublen;    }  }  /**   * Updates the buffer.  Called only from the clob classes.   */  static void append(byte []inode, int inodeOffset,		     Store store, StoreTransaction xa,		     char []buffer, int offset, int length)    throws IOException  {    long currentLength = readLong(inode, inodeOffset);    long newLength = currentLength + length;        writeLong(inode, inodeOffset, newLength);    if (newLength <= INLINE_BLOB_SIZE) {      int writeOffset = (int) (inodeOffset + 8 + currentLength);            for (int i = 0; i < length; i++) {	char ch = buffer[offset + i];	inode[writeOffset++] = (byte) (ch >> 8);	inode[writeOffset++] = (byte) (ch);      }    }    else {      // XXX: theoretically deal with case of appending to inline, although      // the blobs are the only writers and will avoid that case.      if (currentLength % FRAGMENT_SIZE != 0) {	long fragAddr = readFragmentAddr(inode, inodeOffset,					 store,					 currentLength);	int fragOffset = (int) (currentLength % FRAGMENT_SIZE);	int sublen = 2 * length;	if (INODE_BLOCK_SIZE - fragOffset < sublen)	  sublen = INODE_BLOCK_SIZE - fragOffset;	store.writeFragment(xa, fragAddr, fragOffset, buffer, offset, sublen);	offset += sublen / 2;	length -= sublen / 2;	currentLength += sublen;      }            while (length > 0) {	int sublen = 2 * length;	if (INODE_BLOCK_SIZE < sublen)	  sublen = INODE_BLOCK_SIZE;	long fragAddr = store.allocateFragment(xa);	writeFragmentAddr(inode, inodeOffset,			  store, xa,			  currentLength, fragAddr);	store.writeFragment(xa, fragAddr, 0, buffer, offset, sublen);		offset += sublen / 2;	length -= sublen / 2;	currentLength += sublen;      }    }  }  private void appendFragment(byte []inode, int inodeOffset,			      Store store, StoreTransaction xa,			      char []buffer, int offset, int length,			      long currentLength)    throws IOException  {    // XXX: theoretically deal with case of appending to inline, although    // the blobs are the only writers and will avoid that case.    while (length > 0 && currentLength < FRAGMENT_MAX) {      if (currentLength % FRAGMENT_SIZE != 0) {	long fragAddr = readFragmentAddr(inode, inodeOffset,					 store,					 currentLength);	if (fragAddr == 0) {	  store.setCorrupted(true);	  	  throw new IllegalStateException(store + " inode: illegal fragment at " + currentLength);	}	int fragOffset = (int) (currentLength % INODE_BLOCK_SIZE);	int sublen = 2 * length;	if (INODE_BLOCK_SIZE - fragOffset < sublen)	  sublen = INODE_BLOCK_SIZE - fragOffset;	store.writeFragment(xa, fragAddr, fragOffset, buffer, offset, sublen);	offset += sublen / 2;	length -= sublen / 2;	currentLength += sublen;      }      else {	int sublen = 2 * length;	if (FRAGMENT_SIZE < sublen)	  sublen = FRAGMENT_SIZE;	long fragAddr = store.allocateFragment(xa);	if (fragAddr == 0) {	  store.setCorrupted(true);	  	  throw new IllegalStateException(L.l("{0}: illegal fragment",					      store));	}	writeFragmentAddr(inode, inodeOffset,			  store, xa,			  currentLength, fragAddr);	store.writeFragment(xa, fragAddr, 0, buffer, offset, sublen);		offset += sublen / 2;	length -= sublen / 2;	currentLength += sublen;      }    }  }  private void appendBlock(byte []inode, int inodeOffset,			   Store store, StoreTransaction xa,			   char []buffer, int offset, int length,			   long currentLength)    throws IOException  {    // XXX: theoretically deal with case of appending to inline, although    // the blobs are the only writers and will avoid that case.    while (length > 0) {      if ((currentLength - FRAGMENT_MAX) % BLOCK_SIZE != 0) {	long addr = readBlockAddr(inode, inodeOffset,				  store,				  currentLength);	if (addr == 0) {	  store.setCorrupted(true);	  	  throw new IllegalStateException(store + " inode: illegal block at " + currentLength);	}	int blockOffset = (int) ((currentLength - FRAGMENT_MAX) % BLOCK_SIZE);	int sublen = 2 * length;	if (BLOCK_SIZE - blockOffset < sublen)	  sublen = BLOCK_SIZE - blockOffset;	store.writeBlock(xa, addr, blockOffset, buffer, offset, sublen);	offset += sublen / 2;	length -= sublen / 2;	currentLength += sublen;      }      else {	int sublen = 2 * length;	if (BLOCK_SIZE < sublen)	  sublen = BLOCK_SIZE;	long blockAddr = store.allocateFragment(xa);	if (blockAddr == 0) {	  store.setCorrupted(true);	  throw new IllegalStateException(L.l("{0}: illegal fragment",					      store));	}	writeBlockAddr(inode, inodeOffset,		       store, xa,		       currentLength, blockAddr);	store.writeBlock(xa, blockAddr, 0, buffer, offset, sublen);		offset += sublen / 2;	length -= sublen / 2;	currentLength += sublen;      }    }  }  /**   * Opens a byte output stream to the inode.   */  public OutputStream openOutputStream()  {    return new BlobOutputStream(this);  }  /**   * Closes the output stream.   */  void closeOutputStream()  {    try {      _store.saveAllocation();    } catch (Throwable e) {      log.log(Level.FINER, e.toString(), e);    }  }  /**   * Opens a char reader to the inode.   */  public Reader openReader()  {    return new ClobReader(this);  }  /**   * Opens a char writer to the inode.   */  public Writer openWriter()  {    return new ClobWriter(this);  }  /**   * Deletes the inode   */

⌨️ 快捷键说明

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