diff options
Diffstat (limited to 'gerrit-patch-jgit/src/main/java/org/eclipse/jgit/lib/ObjectIdSerialization.java')
-rw-r--r-- | gerrit-patch-jgit/src/main/java/org/eclipse/jgit/lib/ObjectIdSerialization.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/gerrit-patch-jgit/src/main/java/org/eclipse/jgit/lib/ObjectIdSerialization.java b/gerrit-patch-jgit/src/main/java/org/eclipse/jgit/lib/ObjectIdSerialization.java new file mode 100644 index 0000000000..68a0fd4309 --- /dev/null +++ b/gerrit-patch-jgit/src/main/java/org/eclipse/jgit/lib/ObjectIdSerialization.java @@ -0,0 +1,58 @@ +// Copyright (C) 2009 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.eclipse.jgit.lib; + +import org.eclipse.jgit.util.NB; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class ObjectIdSerialization { + public static void writeCanBeNull(final OutputStream out, final AnyObjectId id) + throws IOException { + if (id != null) { + out.write((byte)1); + writeNotNull(out, id); + } else { + out.write((byte)0); + } + } + + public static void writeNotNull(final OutputStream out, final AnyObjectId id) + throws IOException { + id.copyRawTo(out); + } + + public static ObjectId readCanBeNull(final InputStream in) throws IOException { + switch (in.read()) { + case 0: + return null; + case 1: + return readNotNull(in); + default: + throw new IOException("Invalid flag before ObjectId"); + } + } + + public static ObjectId readNotNull(final InputStream in) throws IOException { + final byte[] b = new byte[20]; + NB.readFully(in, b, 0, 20); + return ObjectId.fromRaw(b); + } + + private ObjectIdSerialization() { + } +} |