Java Code Examples for com.intellij.util.io.DataInputOutputUtil#writeINT()

The following examples show how to use com.intellij.util.io.DataInputOutputUtil#writeINT() . 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 File: StubSerializationHelper.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void serializeStubList(StubList stubList, DataOutput out, AbstractStringEnumerator storage, IntEnumerator serializerLocalEnumerator) throws IOException {
  if (!stubList.isChildrenLayoutOptimal()) {
    throw new IllegalArgumentException("Manually assembled stubs should be normalized before serialization, consider wrapping them into StubTree");
  }

  DataInputOutputUtil.writeINT(out, stubList.size());
  DataInputOutputUtil.writeINT(out, stubList.getChildrenCount(0));

  BufferExposingByteArrayOutputStream tempBuffer = new BufferExposingByteArrayOutputStream();
  ByteArrayInterner interner = new ByteArrayInterner();

  for (int i = 1; i < stubList.size(); i++) {
    StubBase<?> stub = stubList.get(i);
    ObjectStubSerializer<Stub, Stub> serializer = writeSerializerId(stub, out, serializerLocalEnumerator);
    DataInputOutputUtil.writeINT(out, interner.internBytes(serializeStub(serializer, storage, stub, tempBuffer)));
    DataInputOutputUtil.writeINT(out, stubList.getChildrenCount(stub.id));
  }

  writeByteArray(out, interner.joinedBuffer.getInternalBuffer(), interner.joinedBuffer.size());
}
 
Example 2
Source File: VfsDependentEnum.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void saveToFile(@Nonnull T instance) throws IOException {
  FileOutputStream fileOutputStream = new FileOutputStream(myFile, true);

  try (DataOutputStream output = new DataOutputStream(new BufferedOutputStream(fileOutputStream))) {
    if (myFile.length() == 0) {
      DataInputOutputUtil.writeTIME(output, FSRecords.getCreationTimestamp());
      DataInputOutputUtil.writeINT(output, myVersion);
    }
    myKeyDescriptor.save(output, instance);
  }
  finally {
    try {
      fileOutputStream.getFD().sync();
    }
    catch (IOException ignored) {
    }
  }
}
 
Example 3
Source File: CompressionUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static int writeCompressed(@Nonnull DataOutput out, @Nonnull byte[] bytes, int start, int length) throws IOException {
  if (length > COMPRESSION_THRESHOLD) {
    LZ4Compressor compressor = compressor();

    byte[] compressedOutputBuffer = spareBufferLocal.getBuffer(compressor.maxCompressedLength(length));
    int compressedSize = compressor.compress(bytes, start, length, compressedOutputBuffer, 0);
    if (compressedSize < length) {
      DataInputOutputUtil.writeINT(out, -compressedSize);
      DataInputOutputUtil.writeINT(out, length - compressedSize);
      out.write(compressedOutputBuffer, 0, compressedSize);
      return compressedSize;
    }
  }
  DataInputOutputUtil.writeINT(out, length);
  out.write(bytes, start, length);
  return length;
}
 
Example 4
Source File: CompressionUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static int writeCompressedWithoutOriginalBufferLength(@Nonnull DataOutput out, @Nonnull byte[] bytes, int length) throws IOException {
  long started = DUMP_COMPRESSION_STATS ? System.nanoTime() : 0;

  LZ4Compressor compressor = compressor();
  final byte[] compressedOutputBuffer = spareBufferLocal.getBuffer(compressor.maxCompressedLength(length));
  int compressedSize = compressor.compress(bytes, 0, length, compressedOutputBuffer, 0);

  final long time = (DUMP_COMPRESSION_STATS ? System.nanoTime() : 0) - started;
  mySizeAfterCompression.addAndGet(compressedSize);
  mySizeBeforeCompression.addAndGet(length);
  int requests = myCompressionRequests.incrementAndGet();
  long l = myCompressionTime.addAndGet(time);

  if (DUMP_COMPRESSION_STATS && (requests & 0x1fff) == 0) {
    System.out.println("Compressed " + requests + " times, size:" + mySizeBeforeCompression + "->" + mySizeAfterCompression + " for " + (l / 1000000) + "ms");
  }

  DataInputOutputUtil.writeINT(out, compressedSize);
  out.write(compressedOutputBuffer, 0, compressedSize);

  return compressedSize;
}
 
Example 5
Source File: ChangeTrackingValueContainer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void saveTo(DataOutput out, DataExternalizer<? super Value> externalizer) throws IOException {
  if (needsCompacting()) {
    getMergedData().saveTo(out, externalizer);
  }
  else {
    final TIntHashSet set = myInvalidated;
    if (set != null && set.size() > 0) {
      for (int inputId : set.toArray()) {
        DataInputOutputUtil.writeINT(out, -inputId); // mark inputId as invalid, to be processed on load in ValueContainerImpl.readFrom
      }
    }

    final UpdatableValueContainer<Value> toAppend = myAdded;
    if (toAppend != null && toAppend.size() > 0) {
      toAppend.saveTo(out, externalizer);
    }
  }
}
 
Example 6
Source File: DuplicatesIndex.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void save(@Nonnull DataOutput out, TIntArrayList list) throws IOException {
  if (list.size() == 2) {
    DataInputOutputUtil.writeINT(out, list.getQuick(0));
    DataInputOutputUtil.writeINT(out, list.getQuick(1));
  }
  else {
    DataInputOutputUtil.writeINT(out, -list.size());
    int prev = 0;
    for (int i = 0, len = list.size(); i < len; i+=2) {
      int value = list.getQuick(i);
      DataInputOutputUtil.writeINT(out, value - prev);
      prev = value;
      DataInputOutputUtil.writeINT(out, list.getQuick(i + 1));
    }
  }
}
 
Example 7
Source File: StubUpdatingIndex.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void rememberIndexingStamp(@Nonnull VirtualFile file, boolean isBinary, long contentByteLength, int contentCharLength) {
  try (DataOutputStream stream = INDEXED_STAMP.writeAttribute(file)) {
    DataInputOutputUtil.writeTIME(stream, file.getTimeStamp());
    DataInputOutputUtil.writeLONG(stream, contentByteLength);

    boolean lengthsAreTheSame = contentByteLength == contentCharLength;
    byte flags = 0;
    flags = BitUtil.set(flags, IS_BINARY_MASK, isBinary);
    flags = BitUtil.set(flags, BYTE_AND_CHAR_LENGTHS_ARE_THE_SAME_MASK, lengthsAreTheSame);
    stream.writeByte(flags);

    if (!lengthsAreTheSame && !isBinary) {
      DataInputOutputUtil.writeINT(stream, contentCharLength);
    }
  }
  catch (IOException e) {
    LOG.error(e);
  }
}
 
Example 8
Source File: ValueContainerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void saveTo(DataOutput out, DataExternalizer<? super Value> externalizer) throws IOException {
  DataInputOutputUtil.writeINT(out, size());

  for (final InvertedIndexValueIterator<Value> valueIterator = getValueIterator(); valueIterator.hasNext(); ) {
    final Value value = valueIterator.next();
    externalizer.save(out, value);
    Object fileSetObject = valueIterator.getFileSetObject();

    if (fileSetObject instanceof Integer) {
      DataInputOutputUtil.writeINT(out, (Integer)fileSetObject); // most common 90% case during index building
    }
    else {
      // serialize positive file ids with delta encoding
      ChangeBufferingList originalInput = (ChangeBufferingList)fileSetObject;
      IntIdsIterator intIterator = originalInput.sortedIntIterator();
      if (DebugAssertions.DEBUG) DebugAssertions.assertTrue(intIterator.hasAscendingOrder());

      if (intIterator.size() == 1) {
        DataInputOutputUtil.writeINT(out, intIterator.next());
      }
      else {
        DataInputOutputUtil.writeINT(out, -intIterator.size());
        int prev = 0;

        while (intIterator.hasNext()) {
          int fileId = intIterator.next();
          DataInputOutputUtil.writeINT(out, fileId - prev);
          prev = fileId;
        }
      }
    }
  }
}
 
Example 9
Source File: StubVersionMap.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void persistIndexedState(int fileId, @Nonnull VirtualFile file) throws IOException {
  try (DataOutputStream stream = FSRecords.writeAttribute(fileId, VERSION_STAMP)) {
    FileType[] type = {null};
    ProgressManager.getInstance().executeNonCancelableSection(() -> {
      type[0] = file.getFileType();
    });
    DataInputOutputUtil.writeINT(stream, getIndexingTimestampDiffForFileType(type[0]));
  }
}
 
Example 10
Source File: StubSerializationHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
void serialize(@Nonnull Stub rootStub, @Nonnull OutputStream stream) throws IOException {
  BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream();
  FileLocalStringEnumerator storage = new FileLocalStringEnumerator(true);
  IntEnumerator selializerIdLocalEnumerator = new IntEnumerator();
  StubOutputStream stubOutputStream = new StubOutputStream(out, storage);
  boolean doDefaultSerialization = true;

  if (rootStub instanceof PsiFileStubImpl) {
    final PsiFileStub[] roots = ((PsiFileStubImpl<?>)rootStub).getStubRoots();
    if (roots.length == 0) {
      Logger.getInstance(getClass()).error("Incorrect stub files count during serialization:" + rootStub + "," + rootStub.getStubType());
    }
    else {
      doDefaultSerialization = false;
      DataInputOutputUtil.writeINT(stubOutputStream, roots.length);
      for (PsiFileStub root : roots) {
        serializeRoot(stubOutputStream, root, storage, selializerIdLocalEnumerator);
      }
    }
  }

  if (doDefaultSerialization) {
    DataInputOutputUtil.writeINT(stubOutputStream, 1);
    serializeRoot(stubOutputStream, rootStub, storage, selializerIdLocalEnumerator);
  }
  DataOutputStream resultStream = new DataOutputStream(stream);
  selializerIdLocalEnumerator.dump(resultStream);
  storage.write(resultStream);
  resultStream.write(out.getInternalBuffer(), 0, out.size());
}
 
Example 11
Source File: StubSerializationHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void serializeChildren(@Nonnull Stub parent, @Nonnull StubOutputStream stream, IntEnumerator serializerLocalEnumerator) throws IOException {
  final List<? extends Stub> children = parent.getChildrenStubs();
  DataInputOutputUtil.writeINT(stream, children.size());
  for (Stub child : children) {
    serializeSelf(child, stream, serializerLocalEnumerator);
    serializeChildren(child, stream, serializerLocalEnumerator);
  }
}
 
Example 12
Source File: SerializedStubTreeDataExternalizer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public final void save(@Nonnull final DataOutput out, @Nonnull final SerializedStubTree tree) throws IOException {
  if (PersistentHashMapValueStorage.COMPRESSION_ENABLED) {
    DataInputOutputUtil.writeINT(out, tree.myTreeByteLength);
    out.write(tree.myTreeBytes, 0, tree.myTreeByteLength);
    if (myIncludeInputs) {
      DataInputOutputUtil.writeINT(out, tree.myIndexedStubByteLength);
      out.write(tree.myIndexedStubBytes, 0, tree.myIndexedStubByteLength);
    }
  }
  else {
    CompressionUtil.writeCompressed(out, tree.myTreeBytes, 0, tree.myTreeByteLength);
    if (myIncludeInputs) CompressionUtil.writeCompressed(out, tree.myIndexedStubBytes, 0, tree.myIndexedStubByteLength);
  }
}
 
Example 13
Source File: InputIndexDataExternalizer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void save(@Nonnull DataOutput out, @Nonnull Collection<K> value) throws IOException {
  try {
    DataInputOutputUtil.writeINT(out, value.size());
    for (K key : value) {
      myKeyDescriptor.save(out, key);
    }
  }
  catch (IllegalArgumentException e) {
    throw new IOException("Error saving data for index " + myIndexId, e);
  }
}
 
Example 14
Source File: CompressionUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Object compressStringRawBytes(@Nonnull CharSequence string) {
  int length = string.length();
  if (length < STRING_COMPRESSION_THRESHOLD) {
    if (string instanceof CharBuffer && ((CharBuffer)string).capacity() > STRING_COMPRESSION_THRESHOLD) {
      string = string.toString();   // shrink to size
    }
    return string;
  }
  try {
    BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(length);
    @Nonnull DataOutput out = new DataOutputStream(bytes);

    for (int i = 0; i < length; i++) {
      char c = string.charAt(i);
      DataInputOutputUtil.writeINT(out, c);
    }

    LZ4Compressor compressor = compressor();
    int bytesWritten = bytes.size();
    ByteBuffer dest = ByteBuffer.wrap(spareBufferLocal.getBuffer(compressor.maxCompressedLength(bytesWritten) + 10));
    DataInputOutputUtil.writeINT(dest, length);
    DataInputOutputUtil.writeINT(dest, bytesWritten - length);
    compressor.compress(ByteBuffer.wrap(bytes.getInternalBuffer(), 0, bytesWritten), dest);

    return dest.position() < length * 2 ? Arrays.copyOf(dest.array(), dest.position()) : string;
  }
  catch (IOException e) {
    e.printStackTrace();
    return string;
  }
}
 
Example 15
Source File: StubForwardIndexExternalizer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void save(@Nonnull DataOutput out, Map<StubIndexKey, Map<Object, StubIdList>> indexedStubs) throws IOException {

  DataInputOutputUtil.writeINT(out, indexedStubs.size());
  if (!indexedStubs.isEmpty()) {
    StubKeySerializationState stubKeySerializationState = createStubIndexKeySerializationState(out, indexedStubs.keySet());

    StubIndexImpl stubIndex = (StubIndexImpl)StubIndex.getInstance();
    for (StubIndexKey stubIndexKey : indexedStubs.keySet()) {
      writeStubIndexKey(out, stubIndexKey, stubKeySerializationState);
      Map<Object, StubIdList> map = indexedStubs.get(stubIndexKey);
      stubIndex.serializeIndexValue(out, stubIndexKey, map);
    }
  }
}
 
Example 16
Source File: PersistentIndicesConfiguration.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void saveConfiguration() {
  try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(indicesConfigurationFile())))) {
    DataInputOutputUtil.writeINT(out, INDICES_CONFIGURATION_VERSION);
    IndexingStamp.savePersistentIndexStamp(out);
  }
  catch (IOException ignored) {
  }
}
 
Example 17
Source File: StubSerializationHelper.java    From consulo with Apache License 2.0 4 votes vote down vote up
private ObjectStubSerializer<Stub, Stub> writeSerializerId(Stub stub, @Nonnull DataOutput stream, IntEnumerator serializerLocalEnumerator) throws IOException {
  ObjectStubSerializer<Stub, Stub> serializer = StubSerializationUtil.getSerializer(stub);
  DataInputOutputUtil.writeINT(stream, serializerLocalEnumerator.enumerate(getClassId(serializer)));
  return serializer;
}
 
Example 18
Source File: StubForwardIndexExternalizer.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeStubIndexKey(@Nonnull DataOutput out, @Nonnull StubIndexKey key, Void aVoid) throws IOException {
  DataInputOutputUtil.writeINT(out, key.getUniqueId());
}
 
Example 19
Source File: StubOutputStream.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void writeName(@Nullable final String arg) throws IOException {
  final int nameId = arg != null ? myNameStorage.enumerate(arg) : 0;
  DataInputOutputUtil.writeINT(this, nameId);
}
 
Example 20
Source File: IndexingStamp.java    From consulo with Apache License 2.0 4 votes vote down vote up
void write(DataOutput os) throws IOException {
  DataInputOutputUtil.writeINT(os, myIndexVersion);
  DataInputOutputUtil.writeINT(os, myCommonIndicesVersion);
  DataInputOutputUtil.writeTIME(os, myVfsCreationStamp);
  DataInputOutputUtil.writeTIME(os, myModificationCount);
}