Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PacketLineOut.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 java.io.IOException;
  47. import java.io.OutputStream;
  48. import org.eclipse.jgit.lib.Constants;
  49. import org.eclipse.jgit.util.RawParseUtils;
  50. import org.slf4j.Logger;
  51. import org.slf4j.LoggerFactory;
  52. /**
  53. * Write Git style pkt-line formatting to an output stream.
  54. * <p>
  55. * This class is not thread safe and may issue multiple writes to the underlying
  56. * stream for each method call made.
  57. * <p>
  58. * This class performs no buffering on its own. This makes it suitable to
  59. * interleave writes performed by this class with writes performed directly
  60. * against the underlying OutputStream.
  61. */
  62. public class PacketLineOut {
  63. private static final Logger log = LoggerFactory.getLogger(PacketLineOut.class);
  64. private final OutputStream out;
  65. private final byte[] lenbuffer;
  66. private boolean flushOnEnd;
  67. /**
  68. * Create a new packet line writer.
  69. *
  70. * @param outputStream
  71. * stream.
  72. */
  73. public PacketLineOut(final OutputStream outputStream) {
  74. out = outputStream;
  75. lenbuffer = new byte[5];
  76. flushOnEnd = true;
  77. }
  78. /**
  79. * Set the flush behavior during {@link #end()}.
  80. *
  81. * @param flushOnEnd
  82. * if true, a flush-pkt written during {@link #end()} also
  83. * flushes the underlying stream.
  84. */
  85. public void setFlushOnEnd(boolean flushOnEnd) {
  86. this.flushOnEnd = flushOnEnd;
  87. }
  88. /**
  89. * Write a UTF-8 encoded string as a single length-delimited packet.
  90. *
  91. * @param s
  92. * string to write.
  93. * @throws IOException
  94. * the packet could not be written, the stream is corrupted as
  95. * the packet may have been only partially written.
  96. */
  97. public void writeString(final String s) throws IOException {
  98. writePacket(Constants.encode(s));
  99. }
  100. /**
  101. * Write a binary packet to the stream.
  102. *
  103. * @param packet
  104. * the packet to write; the length of the packet is equal to the
  105. * size of the byte array.
  106. * @throws IOException
  107. * the packet could not be written, the stream is corrupted as
  108. * the packet may have been only partially written.
  109. */
  110. public void writePacket(byte[] packet) throws IOException {
  111. writePacket(packet, 0, packet.length);
  112. }
  113. /**
  114. * Write a binary packet to the stream.
  115. *
  116. * @param buf
  117. * the packet to write
  118. * @param pos
  119. * first index within {@code buf}.
  120. * @param len
  121. * number of bytes to write.
  122. * @throws IOException
  123. * the packet could not be written, the stream is corrupted as
  124. * the packet may have been only partially written.
  125. * @since 4.5
  126. */
  127. public void writePacket(byte[] buf, int pos, int len) throws IOException {
  128. formatLength(len + 4);
  129. out.write(lenbuffer, 0, 4);
  130. out.write(buf, pos, len);
  131. if (log.isDebugEnabled()) {
  132. String s = RawParseUtils.decode(Constants.CHARSET, buf, pos, len);
  133. log.debug("git> " + s); //$NON-NLS-1$
  134. }
  135. }
  136. /**
  137. * Write a packet end marker, sometimes referred to as a flush command.
  138. * <p>
  139. * Technically this is a magical packet type which can be detected
  140. * separately from an empty string or an empty packet.
  141. * <p>
  142. * Implicitly performs a flush on the underlying OutputStream to ensure the
  143. * peer will receive all data written thus far.
  144. *
  145. * @throws IOException
  146. * the end marker could not be written, the stream is corrupted
  147. * as the end marker may have been only partially written.
  148. */
  149. public void end() throws IOException {
  150. formatLength(0);
  151. out.write(lenbuffer, 0, 4);
  152. log.debug("git> 0000"); //$NON-NLS-1$
  153. if (flushOnEnd)
  154. flush();
  155. }
  156. /**
  157. * Flush the underlying OutputStream.
  158. * <p>
  159. * Performs a flush on the underlying OutputStream to ensure the peer will
  160. * receive all data written thus far.
  161. *
  162. * @throws IOException
  163. * the underlying stream failed to flush.
  164. */
  165. public void flush() throws IOException {
  166. out.flush();
  167. }
  168. private static final byte[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
  169. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  170. private void formatLength(int w) {
  171. formatLength(lenbuffer, w);
  172. }
  173. static void formatLength(byte[] lenbuffer, int w) {
  174. int o = 3;
  175. while (o >= 0 && w != 0) {
  176. lenbuffer[o--] = hexchar[w & 0xf];
  177. w >>>= 4;
  178. }
  179. while (o >= 0)
  180. lenbuffer[o--] = '0';
  181. }
  182. }