stubs: inline logic for data externalization if compression is disabled
[idea/community.git] / platform / indexing-impl / src / com / intellij / psi / stubs / SerializedStubTreeDataExternalizer.java
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.psi.stubs;
3
4 import com.intellij.util.io.DataExternalizer;
5 import com.intellij.util.io.DataInputOutputUtil;
6 import org.jetbrains.annotations.NotNull;
7
8 import java.io.DataInput;
9 import java.io.DataOutput;
10 import java.io.IOException;
11
12 public class SerializedStubTreeDataExternalizer implements DataExternalizer<SerializedStubTree> {
13   @NotNull
14   private final SerializationManagerEx mySerializationManager;
15   private final StubForwardIndexExternalizer<?> myStubIndexesExternalizer;
16
17   public SerializedStubTreeDataExternalizer(@NotNull SerializationManagerEx manager, @NotNull StubForwardIndexExternalizer<?> externalizer) {
18     mySerializationManager = manager;
19     myStubIndexesExternalizer = externalizer;
20   }
21
22   @Override
23   public final void save(@NotNull final DataOutput out, @NotNull final SerializedStubTree tree) throws IOException {
24     DataInputOutputUtil.writeINT(out, tree.myTreeByteLength);
25     out.write(tree.myTreeBytes, 0, tree.myTreeByteLength);
26     DataInputOutputUtil.writeINT(out, tree.myIndexedStubByteLength);
27     out.write(tree.myIndexedStubBytes, 0, tree.myIndexedStubByteLength);
28   }
29
30   @NotNull
31   @Override
32   public final SerializedStubTree read(@NotNull final DataInput in) throws IOException {
33     {
34       int serializedStubsLength = DataInputOutputUtil.readINT(in);
35       byte[] bytes = new byte[serializedStubsLength];
36       in.readFully(bytes);
37       int indexedStubByteLength;
38       byte[] indexedStubBytes;
39       indexedStubByteLength = DataInputOutputUtil.readINT(in);
40       indexedStubBytes = new byte[indexedStubByteLength];
41       in.readFully(indexedStubBytes);
42       return new SerializedStubTree(bytes, bytes.length, indexedStubBytes, indexedStubByteLength,
43                                     null, myStubIndexesExternalizer, mySerializationManager);
44     }
45   }
46 }