You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectIdSerializer.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2009, The Android Open Source Project
  3. * Copyright (C) 2009, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2018, David Pursehouse <david.pursehouse@gmail.com>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import org.eclipse.jgit.annotations.NonNull;
  51. import org.eclipse.jgit.annotations.Nullable;
  52. import org.eclipse.jgit.util.IO;
  53. /**
  54. * Helper to serialize {@link ObjectId} instances. {@link ObjectId} is already
  55. * serializable, but this class provides methods to handle null and non-null
  56. * instances.
  57. *
  58. * @since 4.11
  59. */
  60. public class ObjectIdSerializer {
  61. /*
  62. * Marker to indicate a null ObjectId instance.
  63. */
  64. private static final byte NULL_MARKER = 0;
  65. /*
  66. * Marker to indicate a non-null ObjectId instance.
  67. */
  68. private static final byte NON_NULL_MARKER = 1;
  69. /**
  70. * Write a possibly null {@link ObjectId} to the stream, using markers to
  71. * differentiate null and non-null instances.
  72. *
  73. * <p>
  74. * If the id is non-null, writes a {@link #NON_NULL_MARKER} followed by the
  75. * id's words. If it is null, writes a {@link #NULL_MARKER} and nothing
  76. * else.
  77. *
  78. * @param out
  79. * the output stream
  80. * @param id
  81. * the object id to serialize; may be null
  82. * @throws IOException
  83. * the stream writing failed
  84. */
  85. public static void write(OutputStream out, @Nullable AnyObjectId id)
  86. throws IOException {
  87. if (id != null) {
  88. out.write(NON_NULL_MARKER);
  89. writeWithoutMarker(out, id);
  90. } else {
  91. out.write(NULL_MARKER);
  92. }
  93. }
  94. /**
  95. * Write a non-null {@link ObjectId} to the stream.
  96. *
  97. * @param out
  98. * the output stream
  99. * @param id
  100. * the object id to serialize; never null
  101. * @throws IOException
  102. * the stream writing failed
  103. * @since 4.11
  104. */
  105. public static void writeWithoutMarker(OutputStream out, @NonNull AnyObjectId id)
  106. throws IOException {
  107. id.copyRawTo(out);
  108. }
  109. /**
  110. * Read a possibly null {@link ObjectId} from the stream.
  111. *
  112. * Reads the first byte of the stream, which is expected to be either
  113. * {@link #NON_NULL_MARKER} or {@link #NULL_MARKER}.
  114. *
  115. * @param in
  116. * the input stream
  117. * @return the object id, or null
  118. * @throws IOException
  119. * there was an error reading the stream
  120. */
  121. @Nullable
  122. public static ObjectId read(InputStream in) throws IOException {
  123. byte marker = (byte) in.read();
  124. switch (marker) {
  125. case NULL_MARKER:
  126. return null;
  127. case NON_NULL_MARKER:
  128. return readWithoutMarker(in);
  129. default:
  130. throw new IOException("Invalid flag before ObjectId: " + marker); //$NON-NLS-1$
  131. }
  132. }
  133. /**
  134. * Read a non-null {@link ObjectId} from the stream.
  135. *
  136. * @param in
  137. * the input stream
  138. * @return the object id; never null
  139. * @throws IOException
  140. * there was an error reading the stream
  141. * @since 4.11
  142. */
  143. @NonNull
  144. public static ObjectId readWithoutMarker(InputStream in) throws IOException {
  145. final byte[] b = new byte[OBJECT_ID_LENGTH];
  146. IO.readFully(in, b, 0, OBJECT_ID_LENGTH);
  147. return ObjectId.fromRaw(b);
  148. }
  149. private ObjectIdSerializer() {
  150. }
  151. }