Java Code Examples for com.intellij.openapi.vfs.newvfs.FileAttribute

The following examples show how to use com.intellij.openapi.vfs.newvfs.FileAttribute. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: consulo   Source File: FSRecords.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
public static DataInputStream readAttributeWithLock(int fileId, @Nonnull FileAttribute att) {
  return readAndHandleErrors(() -> {
    try (DataInputStream stream = readAttribute(fileId, att)) {
      if (stream != null && att.isVersioned()) {
        try {
          int actualVersion = DataInputOutputUtil.readINT(stream);
          if (actualVersion != att.getVersion()) {
            return null;
          }
        }
        catch (IOException e) {
          return null;
        }
      }
      return stream;
    }
  });
}
 
Example 2
Source Project: consulo   Source File: FSRecords.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static DataOutputStream writeAttribute(final int fileId, @Nonnull FileAttribute att) {
  DataOutputStream stream = new AttributeOutputStream(fileId, att);
  if (att.isVersioned()) {
    try {
      DataInputOutputUtil.writeINT(stream, att.getVersion());
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  return stream;
}
 
Example 3
Source Project: consulo   Source File: FSRecords.java    License: Apache License 2.0 4 votes vote down vote up
@Nullable
private static DataInputStream readAttribute(int fileId, @Nonnull FileAttribute attribute) throws IOException {
  checkFileIsValid(fileId);

  int recordId = getAttributeRecordId(fileId);
  if (recordId == 0) return null;
  int encodedAttrId = DbConnection.getAttributeId(attribute.getId());

  Storage storage = getAttributesStorage();

  int page = 0;

  try (DataInputStream attrRefs = storage.readStream(recordId)) {
    if (bulkAttrReadSupport) skipRecordHeader(attrRefs, DbConnection.RESERVED_ATTR_ID, fileId);

    while (attrRefs.available() > 0) {
      final int attIdOnPage = DataInputOutputUtil.readINT(attrRefs);
      final int attrAddressOrSize = DataInputOutputUtil.readINT(attrRefs);

      if (attIdOnPage != encodedAttrId) {
        if (inlineAttributes && attrAddressOrSize < MAX_SMALL_ATTR_SIZE) {
          attrRefs.skipBytes(attrAddressOrSize);
        }
      }
      else {
        if (inlineAttributes && attrAddressOrSize < MAX_SMALL_ATTR_SIZE) {
          byte[] b = new byte[attrAddressOrSize];
          attrRefs.readFully(b);
          return new DataInputStream(new UnsyncByteArrayInputStream(b));
        }
        page = inlineAttributes ? attrAddressOrSize - MAX_SMALL_ATTR_SIZE : attrAddressOrSize;
        break;
      }
    }
  }

  if (page == 0) {
    return null;
  }
  DataInputStream stream = getAttributesStorage().readStream(page);
  if (bulkAttrReadSupport) skipRecordHeader(stream, encodedAttrId, fileId);
  return stream;
}
 
Example 4
Source Project: consulo   Source File: FSRecords.java    License: Apache License 2.0 4 votes vote down vote up
private static int findAttributePage(int fileId, @Nonnull FileAttribute attr, boolean toWrite) throws IOException {
  checkFileIsValid(fileId);

  int recordId = getAttributeRecordId(fileId);
  int encodedAttrId = DbConnection.getAttributeId(attr.getId());
  boolean directoryRecord = false;

  Storage storage = getAttributesStorage();

  if (recordId == 0) {
    if (!toWrite) return 0;

    recordId = storage.createNewRecord();
    setAttributeRecordId(fileId, recordId);
    directoryRecord = true;
  }
  else {
    try (DataInputStream attrRefs = storage.readStream(recordId)) {
      if (bulkAttrReadSupport) skipRecordHeader(attrRefs, DbConnection.RESERVED_ATTR_ID, fileId);

      while (attrRefs.available() > 0) {
        final int attIdOnPage = DataInputOutputUtil.readINT(attrRefs);
        final int attrAddressOrSize = DataInputOutputUtil.readINT(attrRefs);

        if (attIdOnPage == encodedAttrId) {
          if (inlineAttributes) {
            return attrAddressOrSize < MAX_SMALL_ATTR_SIZE ? -recordId : attrAddressOrSize - MAX_SMALL_ATTR_SIZE;
          }
          else {
            return attrAddressOrSize;
          }
        }
        else {
          if (inlineAttributes && attrAddressOrSize < MAX_SMALL_ATTR_SIZE) {
            attrRefs.skipBytes(attrAddressOrSize);
          }
        }
      }
    }
  }

  if (toWrite) {
    try (AbstractStorage.AppenderStream appender = storage.appendStream(recordId)) {
      if (bulkAttrReadSupport) {
        if (directoryRecord) {
          DataInputOutputUtil.writeINT(appender, DbConnection.RESERVED_ATTR_ID);
          DataInputOutputUtil.writeINT(appender, fileId);
        }
      }

      DataInputOutputUtil.writeINT(appender, encodedAttrId);
      int attrAddress = storage.createNewRecord();
      DataInputOutputUtil.writeINT(appender, inlineAttributes ? attrAddress + MAX_SMALL_ATTR_SIZE : attrAddress);
      DbConnection.REASONABLY_SMALL.myAttrPageRequested = true;
      return attrAddress;
    }
    finally {
      DbConnection.REASONABLY_SMALL.myAttrPageRequested = false;
    }
  }

  return 0;
}
 
Example 5
Source Project: consulo   Source File: FSRecords.java    License: Apache License 2.0 4 votes vote down vote up
private AttributeOutputStream(final int fileId, @Nonnull FileAttribute attribute) {
  super(new BufferExposingByteArrayOutputStream());
  myFileId = fileId;
  myAttribute = attribute;
}
 
Example 6
Source Project: consulo   Source File: VirtualFileGistImpl.java    License: Apache License 2.0 4 votes vote down vote up
private FileAttribute getFileAttribute(@Nullable Project project) {
  synchronized (ourAttributes) {
    return ourAttributes.get(Pair.create(myId + (project == null ? "###noProject###" : project.getLocationHash()), myVersion + ourInternalVersion));
  }
}