下面列出了com.intellij.psi.stubs.StubIndexKey#com.intellij.util.io.DataInputOutputUtil 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void save(DataOutput dataOutput, List<Unity3dYMLAsset> list) throws IOException
{
DataInputOutputUtil.writeSeq(dataOutput, list, asset ->
{
dataOutput.writeUTF(asset.getGuild());
dataOutput.writeInt(asset.getStartOffset());
String gameObjectName = asset.getGameObjectName();
dataOutput.writeBoolean(gameObjectName != null);
if(gameObjectName != null)
{
dataOutput.writeUTF(gameObjectName);
}
DataInputOutputUtil.writeSeq(dataOutput, asset.getValues(), it ->
{
dataOutput.writeUTF(it.getName());
dataOutput.writeUTF(it.getValue());
dataOutput.writeInt(it.getOffset());
});
});
}
@Override
public List<Unity3dYMLAsset> read(DataInput dataInput) throws IOException
{
return DataInputOutputUtil.readSeq(dataInput, () ->
{
String guid = dataInput.readUTF();
int startOffset = dataInput.readInt();
boolean gameObjectCheck = dataInput.readBoolean();
String gameObjectName = null;
if(gameObjectCheck)
{
gameObjectName = dataInput.readUTF();
}
List<Unity3dYMLField> values = DataInputOutputUtil.readSeq(dataInput, () ->
{
String name = dataInput.readUTF();
String value = dataInput.readUTF();
int offset = dataInput.readInt();
return new Unity3dYMLField(name, value, offset);
});
return new Unity3dYMLAsset(guid, gameObjectName, startOffset, values);
});
}
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) {
}
}
}
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;
}
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;
}
@Nonnull
public static byte[] readCompressedWithoutOriginalBufferLength(@Nonnull DataInput in, int originalBufferLength) throws IOException {
int size = DataInputOutputUtil.readINT(in);
byte[] bytes = spareBufferLocal.getBuffer(size);
in.readFully(bytes, 0, size);
int decompressedRequests = myDecompressionRequests.incrementAndGet();
long started = DUMP_COMPRESSION_STATS ? System.nanoTime() : 0;
final byte[] decompressedResult = decompressor().decompress(bytes, 0, originalBufferLength);
long doneTime = (DUMP_COMPRESSION_STATS ? System.nanoTime() : 0) - started;
long decompressedSize = myDecompressedSize.addAndGet(size);
long decompressedTime = myDecompressionTime.addAndGet(doneTime);
if (DUMP_COMPRESSION_STATS && (decompressedRequests & 0x1fff) == 0) {
System.out.println("Decompressed " + decompressedRequests + " times, size: " + decompressedSize + " for " + (decompressedTime / 1000000) + "ms");
}
return decompressedResult;
}
@Nonnull
public static CharSequence uncompressStringRawBytes(@Nonnull Object compressed) {
if (compressed instanceof CharSequence) return (CharSequence)compressed;
ByteBuffer buffer = ByteBuffer.wrap((byte[])compressed);
int len = DataInputOutputUtil.readINT(buffer);
int uncompressedLength = DataInputOutputUtil.readINT(buffer) + len;
ByteBuffer dest = ByteBuffer.wrap(spareBufferLocal.getBuffer(uncompressedLength), 0, uncompressedLength);
decompressor().decompress(buffer, dest);
dest.rewind();
char[] chars = new char[len];
for (int i = 0; i < len; i++) {
int c = DataInputOutputUtil.readINT(dest);
chars[i] = (char)c;
}
return StringFactory.createShared(chars);
}
@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);
}
}
}
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);
}
}
private Stub deserializeRoot(StubInputStream inputStream, FileLocalStringEnumerator storage, IntEnumerator serializerLocalEnumerator) throws IOException, SerializerNotFoundException {
ObjectStubSerializer<?, Stub> serializer = getClassById(DataInputOutputUtil.readINT(inputStream), null, serializerLocalEnumerator);
ourRootStubSerializer.set(serializer);
try {
Stub stub = serializer.deserialize(inputStream, null);
if (stub instanceof StubBase) {
deserializeStubList((StubBase)stub, serializer, inputStream, storage, serializerLocalEnumerator);
}
else {
deserializeChildren(inputStream, stub, serializerLocalEnumerator);
}
return stub;
}
finally {
ourRootStubSerializer.set(null);
}
}
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());
}
private void deserializeChildren(StubInputStream stream, Stub parent, IntEnumerator serializerLocalEnumerator) throws IOException, SerializerNotFoundException {
int childCount = DataInputOutputUtil.readINT(stream);
for (int i = 0; i < childCount; i++) {
boolean dangling = false;
int id = DataInputOutputUtil.readINT(stream);
if (id == 0) {
dangling = true;
id = DataInputOutputUtil.readINT(stream);
}
Stub child = getClassById(id, parent, serializerLocalEnumerator).deserialize(stream, parent);
if (dangling) {
((ObjectStubBase)child).markDangling();
}
deserializeChildren(stream, child, serializerLocalEnumerator);
}
}
void dump(DataOutputStream stream, IntUnaryOperator idRemapping) throws IOException {
DataInputOutputUtil.writeINT(stream, myIds.size());
IOException[] exception = new IOException[1];
myIds.forEach(id -> {
try {
int remapped = idRemapping.applyAsInt(id);
if (remapped == 0) {
exception[0] = new IOException("remapping is not found for " + id);
return false;
}
DataInputOutputUtil.writeINT(stream, remapped);
}
catch (IOException e) {
exception[0] = e;
return false;
}
return true;
});
if (exception[0] != null) {
throw exception[0];
}
}
@Override
public Data getFileData(@Nullable Project project, @Nonnull VirtualFile file) {
ApplicationManager.getApplication().assertReadAccessAllowed();
ProgressManager.checkCanceled();
if (!(file instanceof VirtualFileWithId)) return myCalculator.calcData(project, file);
int stamp = PersistentFS.getInstance().getModificationCount(file) + ((GistManagerImpl)GistManager.getInstance()).getReindexCount();
try (DataInputStream stream = getFileAttribute(project).readAttribute(file)) {
if (stream != null && DataInputOutputUtil.readINT(stream) == stamp) {
return stream.readBoolean() ? myExternalizer.read(stream) : null;
}
}
catch (IOException e) {
LOG.error(e);
}
Data result = myCalculator.calcData(project, file);
cacheResult(stamp, result, project, file);
return result;
}
@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));
}
}
}
public DirectoryEntry(DataInput in, @SuppressWarnings("unused") boolean dummy /* to distinguish from general constructor*/) throws IOException {
super(in);
int count = DataInputOutputUtil.readINT(in);
myChildren = new ArrayList<>(count);
while (count-- > 0) {
unsafeAddChild(StreamUtil.readEntry(in));
}
}
@Override
public void write(DataOutput out) throws IOException {
super.write(out);
DataInputOutputUtil.writeINT(out, myChildren.size());
for (Entry child : myChildren) {
StreamUtil.writeEntry(out, child);
}
}
TranslationSourceFileInfo(@Nonnull DataInput in) throws IOException {
final int projCount = DataInputOutputUtil.readINT(in);
for (int idx = 0; idx < projCount; idx++) {
final int projectId = DataInputOutputUtil.readINT(in);
final long stamp = DataInputOutputUtil.readTIME(in);
updateTimestamp(projectId, stamp);
final int pathsCount = DataInputOutputUtil.readINT(in);
for (int i = 0; i < pathsCount; i++) {
final int path = in.readInt();
addOutputPath(projectId, path);
}
}
}
@Nonnull
public static Hash read(@Nonnull DataInput in) throws IOException {
int length = DataInputOutputUtil.readINT(in);
if (length == 0) throw new IOException("Can not read hash: data length is zero");
byte[] buf = new byte[length];
in.readFully(buf);
return new HashImpl(buf);
}
@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;
}
}
@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;
}
}
}
}
}
@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);
}
}
@Nonnull
@Override
public Collection<K> read(@Nonnull DataInput in) throws IOException {
try {
final int size = DataInputOutputUtil.readINT(in);
final List<K> list = new ArrayList<>(size);
for (int idx = 0; idx < size; idx++) {
list.add(myKeyDescriptor.read(in));
}
return list;
}
catch (IllegalArgumentException e) {
throw new IOException("Error reading data for index " + myIndexId, e);
}
}
@Nullable
static IndexingStampInfo getIndexingStampInfo(@Nonnull VirtualFile file) {
try (DataInputStream stream = INDEXED_STAMP.readAttribute(file)) {
if (stream == null) {
return null;
}
long stamp = DataInputOutputUtil.readTIME(stream);
long byteLength = DataInputOutputUtil.readLONG(stream);
byte flags = stream.readByte();
boolean isBinary = BitUtil.isSet(flags, IS_BINARY_MASK);
boolean readOnlyOneLength = BitUtil.isSet(flags, BYTE_AND_CHAR_LENGTHS_ARE_THE_SAME_MASK);
int charLength;
if (isBinary) {
charLength = -1;
}
else if (readOnlyOneLength) {
charLength = (int)byteLength;
}
else {
charLength = DataInputOutputUtil.readINT(stream);
}
return new IndexingStampInfo(stamp, byteLength, charLength);
}
catch (IOException e) {
LOG.error(e);
return null;
}
}
@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);
}
}
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);
}
}
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());
}
private byte[] readByteArray(StubInputStream inputStream) throws IOException {
int length = DataInputOutputUtil.readINT(inputStream);
if (length == 0) return ArrayUtilRt.EMPTY_BYTE_ARRAY;
byte[] array = new byte[length];
int read = inputStream.read(array);
if (read != array.length) {
Logger.getInstance(getClass()).error("Serialized array length mismatch");
}
return array;
}
static void readEnumeratedStrings(@Nonnull FileLocalStringEnumerator enumerator, @Nonnull DataInput stream, @Nonnull UnaryOperator<String> interner) throws IOException {
final int numberOfStrings = DataInputOutputUtil.readINT(stream);
byte[] buffer = IOUtil.allocReadWriteUTFBuffer();
enumerator.myStrings.ensureCapacity(numberOfStrings);
int i = 0;
while (i < numberOfStrings) {
String s = interner.apply(IOUtil.readUTFFast(buffer, stream));
enumerator.myStrings.add(s);
++i;
}
}
static IntEnumerator read(DataInputStream stream) throws IOException {
int size = DataInputOutputUtil.readINT(stream);
IntEnumerator enumerator = new IntEnumerator(false);
for (int i = 1; i < size + 1; i++) {
enumerator.myIds.add(DataInputOutputUtil.readINT(stream));
}
return enumerator;
}