Implement "jgit upload-pack" to support fetching from jgit
[egit/qmx.git] / org.spearce.jgit / src / org / spearce / jgit / transport / SideBandOutputStream.java
blob31c9f5df032c81e704740edbef24b3c6607e3f61
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.transport;
40 import java.io.IOException;
41 import java.io.OutputStream;
43 /**
44 * Multiplexes data and progress messages
45 * <p>
46 * To correctly use this class you must wrap it in a BufferedOutputStream with a
47 * buffer size no larger than either {@link #SMALL_BUF} or {@link #MAX_BUF},
48 * minus {@link #HDR_SIZE}.
50 class SideBandOutputStream extends OutputStream {
51 static final int CH_DATA = SideBandInputStream.CH_DATA;
53 static final int CH_PROGRESS = SideBandInputStream.CH_PROGRESS;
55 static final int CH_ERROR = SideBandInputStream.CH_ERROR;
57 static final int SMALL_BUF = 1000;
59 static final int MAX_BUF = 65520;
61 static final int HDR_SIZE = 5;
63 private final int channel;
65 private final PacketLineOut pckOut;
67 private byte[] singleByteBuffer;
69 SideBandOutputStream(final int chan, final PacketLineOut out) {
70 channel = chan;
71 pckOut = out;
74 @Override
75 public void flush() throws IOException {
76 if (channel != CH_DATA)
77 pckOut.flush();
80 @Override
81 public void write(final byte[] b, final int off, final int len)
82 throws IOException {
83 pckOut.writeChannelPacket(channel, b, off, len);
86 @Override
87 public void write(final int b) throws IOException {
88 if (singleByteBuffer == null)
89 singleByteBuffer = new byte[1];
90 singleByteBuffer[0] = (byte) b;
91 write(singleByteBuffer);