~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Transport/SideBandOutputStream.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit'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
 
 
44
using System;
 
45
using NGit;
 
46
using NGit.Transport;
 
47
using Sharpen;
 
48
 
 
49
namespace NGit.Transport
 
50
{
 
51
        /// <summary>Multiplexes data and progress messages.</summary>
 
52
        /// <remarks>
 
53
        /// Multiplexes data and progress messages.
 
54
        /// <p>
 
55
        /// This stream is buffered at packet sizes, so the caller doesn't need to wrap
 
56
        /// it in yet another buffered stream.
 
57
        /// </remarks>
 
58
        internal class SideBandOutputStream : OutputStream
 
59
        {
 
60
                internal const int CH_DATA = SideBandInputStream.CH_DATA;
 
61
 
 
62
                internal const int CH_PROGRESS = SideBandInputStream.CH_PROGRESS;
 
63
 
 
64
                internal const int CH_ERROR = SideBandInputStream.CH_ERROR;
 
65
 
 
66
                internal const int SMALL_BUF = 1000;
 
67
 
 
68
                internal const int MAX_BUF = 65520;
 
69
 
 
70
                internal const int HDR_SIZE = 5;
 
71
 
 
72
                private readonly OutputStream @out;
 
73
 
 
74
                private readonly byte[] buffer;
 
75
 
 
76
                /// <summary>
 
77
                /// Number of bytes in
 
78
                /// <see cref="buffer">buffer</see>
 
79
                /// that are valid data.
 
80
                /// <p>
 
81
                /// Initialized to
 
82
                /// <see cref="HDR_SIZE">HDR_SIZE</see>
 
83
                /// if there is no application data in the
 
84
                /// buffer, as the packet header always appears at the start of the buffer.
 
85
                /// </summary>
 
86
                private int cnt;
 
87
 
 
88
                /// <summary>Create a new stream to write side band packets.</summary>
 
89
                /// <remarks>Create a new stream to write side band packets.</remarks>
 
90
                /// <param name="chan">
 
91
                /// channel number to prefix all packets with, so the remote side
 
92
                /// can demultiplex the stream and get back the original data.
 
93
                /// Must be in the range [0, 255].
 
94
                /// </param>
 
95
                /// <param name="sz">
 
96
                /// maximum size of a data packet within the stream. The remote
 
97
                /// side needs to agree to the packet size to prevent buffer
 
98
                /// overflows. Must be in the range [HDR_SIZE + 1, MAX_BUF).
 
99
                /// </param>
 
100
                /// <param name="os">
 
101
                /// stream that the packets are written onto. This stream should
 
102
                /// be attached to a SideBandInputStream on the remote side.
 
103
                /// </param>
 
104
                internal SideBandOutputStream(int chan, int sz, OutputStream os)
 
105
                {
 
106
                        if (chan <= 0 || chan > 255)
 
107
                        {
 
108
                                throw new ArgumentException(MessageFormat.Format(JGitText.Get().channelMustBeInRange0_255
 
109
                                        , chan));
 
110
                        }
 
111
                        if (sz <= HDR_SIZE)
 
112
                        {
 
113
                                throw new ArgumentException(MessageFormat.Format(JGitText.Get().packetSizeMustBeAtLeast
 
114
                                        , sz, HDR_SIZE));
 
115
                        }
 
116
                        else
 
117
                        {
 
118
                                if (MAX_BUF < sz)
 
119
                                {
 
120
                                        throw new ArgumentException(MessageFormat.Format(JGitText.Get().packetSizeMustBeAtMost
 
121
                                                , sz, MAX_BUF));
 
122
                                }
 
123
                        }
 
124
                        @out = os;
 
125
                        buffer = new byte[sz];
 
126
                        buffer[4] = unchecked((byte)chan);
 
127
                        cnt = HDR_SIZE;
 
128
                }
 
129
 
 
130
                /// <exception cref="System.IO.IOException"></exception>
 
131
                internal virtual void FlushBuffer()
 
132
                {
 
133
                        if (HDR_SIZE < cnt)
 
134
                        {
 
135
                                WriteBuffer();
 
136
                        }
 
137
                }
 
138
 
 
139
                /// <exception cref="System.IO.IOException"></exception>
 
140
                public override void Flush()
 
141
                {
 
142
                        FlushBuffer();
 
143
                        @out.Flush();
 
144
                }
 
145
 
 
146
                /// <exception cref="System.IO.IOException"></exception>
 
147
                public override void Write(byte[] b, int off, int len)
 
148
                {
 
149
                        while (0 < len)
 
150
                        {
 
151
                                int capacity = buffer.Length - cnt;
 
152
                                if (cnt == HDR_SIZE && capacity < len)
 
153
                                {
 
154
                                        // Our block to write is bigger than the packet size,
 
155
                                        // stream it out as-is to avoid unnecessary copies.
 
156
                                        PacketLineOut.FormatLength(buffer, buffer.Length);
 
157
                                        @out.Write(buffer, 0, HDR_SIZE);
 
158
                                        @out.Write(b, off, capacity);
 
159
                                        off += capacity;
 
160
                                        len -= capacity;
 
161
                                }
 
162
                                else
 
163
                                {
 
164
                                        if (capacity == 0)
 
165
                                        {
 
166
                                                WriteBuffer();
 
167
                                        }
 
168
                                        int n = Math.Min(len, capacity);
 
169
                                        System.Array.Copy(b, off, buffer, cnt, n);
 
170
                                        cnt += n;
 
171
                                        off += n;
 
172
                                        len -= n;
 
173
                                }
 
174
                        }
 
175
                }
 
176
 
 
177
                /// <exception cref="System.IO.IOException"></exception>
 
178
                public override void Write(int b)
 
179
                {
 
180
                        if (cnt == buffer.Length)
 
181
                        {
 
182
                                WriteBuffer();
 
183
                        }
 
184
                        buffer[cnt++] = unchecked((byte)b);
 
185
                }
 
186
 
 
187
                /// <exception cref="System.IO.IOException"></exception>
 
188
                private void WriteBuffer()
 
189
                {
 
190
                        PacketLineOut.FormatLength(buffer, cnt);
 
191
                        @out.Write(buffer, 0, cnt);
 
192
                        cnt = HDR_SIZE;
 
193
                }
 
194
        }
 
195
}