1 // Copyright 2000-2020 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 org.jetbrains.jps.builders.java.dependencyView;
4 import com.intellij.util.io.DataExternalizer;
5 import com.intellij.util.io.DataInputOutputUtil;
6 import com.intellij.util.io.IOUtil;
7 import gnu.trove.TIntHashSet;
8 import org.jetbrains.jps.builders.storage.BuildDataCorruptedException;
11 import java.util.Collection;
16 public final class RW {
21 protected static String readUTF(DataInput in) throws IOException {
22 return IOUtil.readUTF(in);
25 protected static void writeUTF(DataOutput out, String value) throws IOException {
26 IOUtil.writeUTF(out, value);
29 public interface Savable {
30 void save(DataOutput out);
33 public static <X extends Savable> void save(final X[] x, final DataOutput out) {
35 DataInputOutputUtil.writeINT(out, x.length);
40 catch (IOException e) {
41 throw new BuildDataCorruptedException(e);
45 public static <X> void save(final TIntHashSet x, final DataOutput out) {
47 DataInputOutputUtil.writeINT(out, x.size());
50 DataInputOutputUtil.writeINT(out, value);
53 catch (IOException e) {
54 throw new BuildDataCorruptedException(e);
58 catch (IOException c) {
59 throw new BuildDataCorruptedException(c);
63 public static <X> void save(final Collection<? extends X> x, final DataExternalizer<X> e, final DataOutput out) {
65 DataInputOutputUtil.writeINT(out, x.size());
71 catch (IOException c) {
72 throw new BuildDataCorruptedException(c);
76 public static <X extends Savable> void save(final Collection<X> x, final DataOutput out) {
78 final int size = x.size();
80 DataInputOutputUtil.writeINT(out, size);
86 catch (IOException e) {
87 throw new BuildDataCorruptedException(e);
91 public static <X> X[] read(final DataExternalizer<X> e, final DataInput in, final X[] result) {
93 for (int i = 0; i < result.length; i++) {
94 result[i] = e.read(in);
99 catch (IOException x) {
100 throw new BuildDataCorruptedException(x);
104 public static TIntHashSet read(final TIntHashSet acc, final DataInput in) {
106 final int size = DataInputOutputUtil.readINT(in);
108 for (int i = 0; i<size; i++) {
109 acc.add(DataInputOutputUtil.readINT(in));
114 catch (IOException x) {
115 throw new BuildDataCorruptedException(x);
119 public static <X,C extends Collection<X>> C read(final DataExternalizer<X> e, final C acc, final DataInput in) {
121 final int size = DataInputOutputUtil.readINT(in);
123 for (int i = 0; i<size; i++) {
129 catch (IOException x) {
130 throw new BuildDataCorruptedException(x);
134 public interface Writable {
135 void write(BufferedWriter w);
138 public interface ToWritable<T> {
139 Writable convert(T x);
142 public static void writeln(final BufferedWriter w, final String s) {
152 catch (IOException e) {
153 throw new BuildDataCorruptedException(e);
157 public interface Reader<T> {
158 T read(BufferedReader r);
161 public static ToWritable<String> fromString = new ToWritable<String>() {
163 public Writable convert(final String s) {
164 return new Writable() {
166 public void write(BufferedWriter w) {
173 public static String readString(final BufferedReader r) {
177 catch (IOException e) {
178 throw new BuildDataCorruptedException(e);
182 public static long readLong(final BufferedReader r) {
183 final String s = readString(r);
186 return Long.parseLong(s);
188 catch (Exception n) {
189 System.err.println("Parsing error: expected long, but found \"" + s + "\"");