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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. */
  104. public static void writeWithoutMarker(OutputStream out, @NonNull AnyObjectId id)
  105. throws IOException {
  106. id.copyRawTo(out);
  107. }
  108. /**
  109. * Read a possibly null {@link ObjectId} from the stream.
  110. *
  111. * Reads the first byte of the stream, which is expected to be either
  112. * {@link #NON_NULL_MARKER} or {@link #NULL_MARKER}.
  113. *
  114. * @param in
  115. * the input stream
  116. * @return the object id, or null
  117. * @throws IOException
  118. * there was an error reading the stream
  119. */
  120. @Nullable
  121. public static ObjectId read(InputStream in) throws IOException {
  122. byte marker = (byte) in.read();
  123. switch (marker) {
  124. case NULL_MARKER:
  125. return null;
  126. case NON_NULL_MARKER:
  127. return readWithoutMarker(in);
  128. default:
  129. throw new IOException("Invalid flag before ObjectId: " + marker); //$NON-NLS-1$
  130. }
  131. }
  132. /**
  133. * Read a non-null {@link ObjectId} from the stream.
  134. *
  135. * @param in
  136. * the input stream
  137. * @return the object id; never null
  138. * @throws IOException
  139. * there was an error reading the stream
  140. */
  141. @NonNull
  142. public static ObjectId readWithoutMarker(InputStream in) throws IOException {
  143. final byte[] b = new byte[OBJECT_ID_LENGTH];
  144. IO.readFully(in, b, 0, OBJECT_ID_LENGTH);
  145. return ObjectId.fromRaw(b);
  146. }
  147. private ObjectIdSerializer() {
  148. }
  149. }