aboutsummaryrefslogtreecommitdiff
path: root/csharp/ProtocolBuffers/MessageStreamWriter.cs
blob: 9ff33b05d6617ede45c370f819978c4784545d9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Google.ProtocolBuffers {
  /// <summary>
  /// Writes multiple messages to the same stream. Each message is written
  /// as if it were an element of a repeated field 1 in a larger protocol buffer.
  /// This class takes no ownership of the stream it is given; it never closes the
  /// stream.
  /// </summary>
  public sealed class MessageStreamWriter<T> where T : IMessage<T> {

    private readonly CodedOutputStream codedOutput;

    /// <summary>
    /// Creates an instance which writes to the given stream.
    /// </summary>
    /// <param name="output">Stream to write messages to.</param>
    public MessageStreamWriter(Stream output) {
      codedOutput = CodedOutputStream.CreateInstance(output);
    }

    public void Write(T message) {
      codedOutput.WriteMessage(1, message);
    }

    public void Flush() {
      codedOutput.Flush();
    }
  }
}