Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SideBandOutputStream.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  47. * Multiplexes data and progress messages.
  48. * <p>
  49. * This stream is buffered at packet sizes, so the caller doesn't need to wrap
  50. * it in yet another buffered stream.
  51. */
  52. class SideBandOutputStream extends OutputStream {
  53. static final int CH_DATA = SideBandInputStream.CH_DATA;
  54. static final int CH_PROGRESS = SideBandInputStream.CH_PROGRESS;
  55. static final int CH_ERROR = SideBandInputStream.CH_ERROR;
  56. static final int SMALL_BUF = 1000;
  57. static final int MAX_BUF = 65520;
  58. static final int HDR_SIZE = 5;
  59. private final OutputStream out;
  60. private final byte[] buffer;
  61. /**
  62. * Number of bytes in {@link #buffer} that are valid data.
  63. * <p>
  64. * Initialized to {@link #HDR_SIZE} if there is no application data in the
  65. * buffer, as the packet header always appears at the start of the buffer.
  66. */
  67. private int cnt;
  68. /**
  69. * Create a new stream to write side band packets.
  70. *
  71. * @param chan
  72. * channel number to prefix all packets with, so the remote side
  73. * can demultiplex the stream and get back the original data.
  74. * Must be in the range [0, 255].
  75. * @param sz
  76. * maximum size of a data packet within the stream. The remote
  77. * side needs to agree to the packet size to prevent buffer
  78. * overflows. Must be in the range [HDR_SIZE + 1, MAX_BUF).
  79. * @param os
  80. * stream that the packets are written onto. This stream should
  81. * be attached to a SideBandInputStream on the remote side.
  82. */
  83. SideBandOutputStream(final int chan, final int sz, final OutputStream os) {
  84. if (chan <= 0 || chan > 255)
  85. throw new IllegalArgumentException("channel " + chan
  86. + " must be in range [0, 255]");
  87. if (sz <= HDR_SIZE)
  88. throw new IllegalArgumentException("packet size " + sz
  89. + " must be >= " + HDR_SIZE);
  90. else if (MAX_BUF < sz)
  91. throw new IllegalArgumentException("packet size " + sz
  92. + " must be <= " + MAX_BUF);
  93. out = os;
  94. buffer = new byte[sz];
  95. buffer[4] = (byte) chan;
  96. cnt = HDR_SIZE;
  97. }
  98. @Override
  99. public void flush() throws IOException {
  100. if (HDR_SIZE < cnt)
  101. writeBuffer();
  102. out.flush();
  103. }
  104. @Override
  105. public void write(final byte[] b, int off, int len) throws IOException {
  106. while (0 < len) {
  107. int capacity = buffer.length - cnt;
  108. if (cnt == HDR_SIZE && capacity < len) {
  109. // Our block to write is bigger than the packet size,
  110. // stream it out as-is to avoid unnecessary copies.
  111. PacketLineOut.formatLength(buffer, buffer.length);
  112. out.write(buffer, 0, HDR_SIZE);
  113. out.write(b, off, capacity);
  114. off += capacity;
  115. len -= capacity;
  116. } else {
  117. if (capacity == 0)
  118. writeBuffer();
  119. int n = Math.min(len, capacity);
  120. System.arraycopy(b, off, buffer, cnt, n);
  121. cnt += n;
  122. off += n;
  123. len -= n;
  124. }
  125. }
  126. }
  127. @Override
  128. public void write(final int b) throws IOException {
  129. if (cnt == buffer.length)
  130. writeBuffer();
  131. buffer[cnt++] = (byte) b;
  132. }
  133. private void writeBuffer() throws IOException {
  134. PacketLineOut.formatLength(buffer, cnt);
  135. out.write(buffer, 0, cnt);
  136. cnt = HDR_SIZE;
  137. }
  138. }