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.

PacketLineOut.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.transport;
  46. import static java.nio.charset.StandardCharsets.UTF_8;
  47. import java.io.IOException;
  48. import java.io.OutputStream;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.util.RawParseUtils;
  51. import org.slf4j.Logger;
  52. import org.slf4j.LoggerFactory;
  53. /**
  54. * Write Git style pkt-line formatting to an output stream.
  55. * <p>
  56. * This class is not thread safe and may issue multiple writes to the underlying
  57. * stream for each method call made.
  58. * <p>
  59. * This class performs no buffering on its own. This makes it suitable to
  60. * interleave writes performed by this class with writes performed directly
  61. * against the underlying OutputStream.
  62. */
  63. public class PacketLineOut {
  64. private static final Logger log = LoggerFactory.getLogger(PacketLineOut.class);
  65. private final OutputStream out;
  66. private final byte[] lenbuffer;
  67. private boolean flushOnEnd;
  68. private boolean usingSideband;
  69. /**
  70. * Create a new packet line writer.
  71. *
  72. * @param outputStream
  73. * stream.
  74. */
  75. public PacketLineOut(OutputStream outputStream) {
  76. out = outputStream;
  77. lenbuffer = new byte[5];
  78. flushOnEnd = true;
  79. }
  80. /**
  81. * Set the flush behavior during {@link #end()}.
  82. *
  83. * @param flushOnEnd
  84. * if true, a flush-pkt written during {@link #end()} also
  85. * flushes the underlying stream.
  86. */
  87. public void setFlushOnEnd(boolean flushOnEnd) {
  88. this.flushOnEnd = flushOnEnd;
  89. }
  90. /**
  91. * @return whether to add a sideband designator to each non-flush and
  92. * non-delim packet
  93. * @see #setUsingSideband
  94. * @since 5.5
  95. */
  96. public boolean isUsingSideband() {
  97. return usingSideband;
  98. }
  99. /**
  100. * @param value If true, when writing packet lines, add, as the first
  101. * byte, a sideband designator to each non-flush and non-delim
  102. * packet. See pack-protocol.txt and protocol-v2.txt from the Git
  103. * project for more information, specifically the "side-band" and
  104. * "sideband-all" sections.
  105. * @since 5.5
  106. */
  107. public void setUsingSideband(boolean value) {
  108. this.usingSideband = value;
  109. }
  110. /**
  111. * Write a UTF-8 encoded string as a single length-delimited packet.
  112. *
  113. * @param s
  114. * string to write.
  115. * @throws java.io.IOException
  116. * the packet could not be written, the stream is corrupted as
  117. * the packet may have been only partially written.
  118. */
  119. public void writeString(String s) throws IOException {
  120. writePacket(Constants.encode(s));
  121. }
  122. /**
  123. * Write a binary packet to the stream.
  124. *
  125. * @param packet
  126. * the packet to write; the length of the packet is equal to the
  127. * size of the byte array.
  128. * @throws java.io.IOException
  129. * the packet could not be written, the stream is corrupted as
  130. * the packet may have been only partially written.
  131. */
  132. public void writePacket(byte[] packet) throws IOException {
  133. writePacket(packet, 0, packet.length);
  134. }
  135. /**
  136. * Write a binary packet to the stream.
  137. *
  138. * @param buf
  139. * the packet to write
  140. * @param pos
  141. * first index within {@code buf}.
  142. * @param len
  143. * number of bytes to write.
  144. * @throws java.io.IOException
  145. * the packet could not be written, the stream is corrupted as
  146. * the packet may have been only partially written.
  147. * @since 4.5
  148. */
  149. public void writePacket(byte[] buf, int pos, int len) throws IOException {
  150. if (usingSideband) {
  151. formatLength(len + 5);
  152. out.write(lenbuffer, 0, 4);
  153. out.write(1);
  154. } else {
  155. formatLength(len + 4);
  156. out.write(lenbuffer, 0, 4);
  157. }
  158. out.write(buf, pos, len);
  159. if (log.isDebugEnabled()) {
  160. String s = RawParseUtils.decode(UTF_8, buf, pos, len);
  161. log.debug("git> " + s); //$NON-NLS-1$
  162. }
  163. }
  164. /**
  165. * Write a packet delim marker (0001).
  166. *
  167. * @throws java.io.IOException
  168. * the marker could not be written, the stream is corrupted
  169. * as the marker may have been only partially written.
  170. * @since 5.0
  171. */
  172. public void writeDelim() throws IOException {
  173. formatLength(1);
  174. out.write(lenbuffer, 0, 4);
  175. log.debug("git> 0001"); //$NON-NLS-1$
  176. }
  177. /**
  178. * Write a packet end marker, sometimes referred to as a flush command.
  179. * <p>
  180. * Technically this is a magical packet type which can be detected
  181. * separately from an empty string or an empty packet.
  182. * <p>
  183. * Implicitly performs a flush on the underlying OutputStream to ensure the
  184. * peer will receive all data written thus far.
  185. *
  186. * @throws java.io.IOException
  187. * the end marker could not be written, the stream is corrupted
  188. * as the end marker may have been only partially written.
  189. */
  190. public void end() throws IOException {
  191. formatLength(0);
  192. out.write(lenbuffer, 0, 4);
  193. log.debug("git> 0000"); //$NON-NLS-1$
  194. if (flushOnEnd)
  195. flush();
  196. }
  197. /**
  198. * Flush the underlying OutputStream.
  199. * <p>
  200. * Performs a flush on the underlying OutputStream to ensure the peer will
  201. * receive all data written thus far.
  202. *
  203. * @throws java.io.IOException
  204. * the underlying stream failed to flush.
  205. */
  206. public void flush() throws IOException {
  207. out.flush();
  208. }
  209. private static final byte[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
  210. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  211. private void formatLength(int w) {
  212. formatLength(lenbuffer, w);
  213. }
  214. static void formatLength(byte[] lenbuffer, int w) {
  215. int o = 3;
  216. while (o >= 0 && w != 0) {
  217. lenbuffer[o--] = hexchar[w & 0xf];
  218. w >>>= 4;
  219. }
  220. while (o >= 0)
  221. lenbuffer[o--] = '0';
  222. }
  223. }