Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.IOException;
  45. import java.io.OutputStream;
  46. import java.text.MessageFormat;
  47. import org.eclipse.jgit.internal.JGitText;
  48. /**
  49. * Multiplexes data and progress messages.
  50. * <p>
  51. * This stream is buffered at packet sizes, so the caller doesn't need to wrap
  52. * it in yet another buffered stream.
  53. *
  54. * @since 2.0
  55. */
  56. public class SideBandOutputStream extends OutputStream {
  57. /** Channel used for pack data. */
  58. public static final int CH_DATA = SideBandInputStream.CH_DATA;
  59. /** Channel used for progress messages. */
  60. public static final int CH_PROGRESS = SideBandInputStream.CH_PROGRESS;
  61. /** Channel used for error messages. */
  62. public static final int CH_ERROR = SideBandInputStream.CH_ERROR;
  63. /** Default buffer size for a small amount of data. */
  64. public static final int SMALL_BUF = 1000;
  65. /** Maximum buffer size for a single packet of sideband data. */
  66. public static final int MAX_BUF = 65520;
  67. static final int HDR_SIZE = 5;
  68. private final OutputStream out;
  69. private final byte[] buffer;
  70. /**
  71. * Number of bytes in {@link #buffer} that are valid data.
  72. * <p>
  73. * Initialized to {@link #HDR_SIZE} if there is no application data in the
  74. * buffer, as the packet header always appears at the start of the buffer.
  75. */
  76. private int cnt;
  77. /**
  78. * Create a new stream to write side band packets.
  79. *
  80. * @param chan
  81. * channel number to prefix all packets with, so the remote side
  82. * can demultiplex the stream and get back the original data.
  83. * Must be in the range [1, 255].
  84. * @param sz
  85. * maximum size of a data packet within the stream. The remote
  86. * side needs to agree to the packet size to prevent buffer
  87. * overflows. Must be in the range [HDR_SIZE + 1, MAX_BUF).
  88. * @param os
  89. * stream that the packets are written onto. This stream should
  90. * be attached to a SideBandInputStream on the remote side.
  91. */
  92. public SideBandOutputStream(int chan, int sz, OutputStream os) {
  93. if (chan <= 0 || chan > 255)
  94. throw new IllegalArgumentException(MessageFormat.format(
  95. JGitText.get().channelMustBeInRange1_255,
  96. Integer.valueOf(chan)));
  97. if (sz <= HDR_SIZE)
  98. throw new IllegalArgumentException(MessageFormat.format(
  99. JGitText.get().packetSizeMustBeAtLeast,
  100. Integer.valueOf(sz), Integer.valueOf(HDR_SIZE)));
  101. else if (MAX_BUF < sz)
  102. throw new IllegalArgumentException(MessageFormat.format(
  103. JGitText.get().packetSizeMustBeAtMost, Integer.valueOf(sz),
  104. Integer.valueOf(MAX_BUF)));
  105. out = os;
  106. buffer = new byte[sz];
  107. buffer[4] = (byte) chan;
  108. cnt = HDR_SIZE;
  109. }
  110. void flushBuffer() throws IOException {
  111. if (HDR_SIZE < cnt)
  112. writeBuffer();
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. public void flush() throws IOException {
  117. flushBuffer();
  118. out.flush();
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. public void write(byte[] b, int off, int len) throws IOException {
  123. while (0 < len) {
  124. int capacity = buffer.length - cnt;
  125. if (cnt == HDR_SIZE && capacity < len) {
  126. // Our block to write is bigger than the packet size,
  127. // stream it out as-is to avoid unnecessary copies.
  128. PacketLineOut.formatLength(buffer, buffer.length);
  129. out.write(buffer, 0, HDR_SIZE);
  130. out.write(b, off, capacity);
  131. off += capacity;
  132. len -= capacity;
  133. } else {
  134. if (capacity == 0)
  135. writeBuffer();
  136. int n = Math.min(len, capacity);
  137. System.arraycopy(b, off, buffer, cnt, n);
  138. cnt += n;
  139. off += n;
  140. len -= n;
  141. }
  142. }
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public void write(int b) throws IOException {
  147. if (cnt == buffer.length)
  148. writeBuffer();
  149. buffer[cnt++] = (byte) b;
  150. }
  151. private void writeBuffer() throws IOException {
  152. PacketLineOut.formatLength(buffer, cnt);
  153. out.write(buffer, 0, cnt);
  154. cnt = HDR_SIZE;
  155. }
  156. }