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.

SideBandOutputStream.java 5.1KB

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