summaryrefslogtreecommitdiffstats
path: root/xpcom/io/FixedBufferOutputStream.cpp
blob: 1c841bb91f0892f2a01301944723af3a4a79018c (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FixedBufferOutputStream.h"

#include "mozilla/RefPtr.h"
#include "mozilla/StreamBufferSinkImpl.h"
#include "nsStreamUtils.h"
#include "nsString.h"

namespace mozilla {

FixedBufferOutputStream::FixedBufferOutputStream(
    UniquePtr<StreamBufferSink>&& aSink)
    : mSink(std::move(aSink)),
      mMutex("FixedBufferOutputStream::mMutex"),
      mOffset(0),
      mWriting(false),
      mClosed(false) {}

// static
RefPtr<FixedBufferOutputStream> FixedBufferOutputStream::Create(
    size_t aLength) {
  MOZ_ASSERT(aLength);

  return MakeRefPtr<FixedBufferOutputStream>(MakeUnique<BufferSink>(aLength));
}

// static
RefPtr<FixedBufferOutputStream> FixedBufferOutputStream::Create(
    size_t aLength, const mozilla::fallible_t&) {
  MOZ_ASSERT(aLength);

  auto sink = BufferSink::Alloc(aLength);

  if (NS_WARN_IF(!sink)) {
    return nullptr;
  }

  return MakeRefPtr<FixedBufferOutputStream>(std::move(sink));
}

// static
RefPtr<FixedBufferOutputStream> FixedBufferOutputStream::Create(
    mozilla::Span<char> aBuffer) {
  return MakeRefPtr<FixedBufferOutputStream>(
      MakeUnique<nsBorrowedSink>(aBuffer));
}

// static
RefPtr<FixedBufferOutputStream> FixedBufferOutputStream::Create(
    UniquePtr<StreamBufferSink>&& aSink) {
  return MakeRefPtr<FixedBufferOutputStream>(std::move(aSink));
}

nsDependentCSubstring FixedBufferOutputStream::WrittenData() {
  MutexAutoLock autoLock(mMutex);

  return mSink->Slice(mOffset);
}

NS_IMPL_ISUPPORTS(FixedBufferOutputStream, nsIOutputStream)

NS_IMETHODIMP
FixedBufferOutputStream::Close() {
  MutexAutoLock autoLock(mMutex);

  if (mWriting) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  mClosed = true;

  return NS_OK;
}

NS_IMETHODIMP
FixedBufferOutputStream::Flush() { return NS_OK; }

NS_IMETHODIMP
FixedBufferOutputStream::StreamStatus() {
  MutexAutoLock autoLock(mMutex);
  return mClosed ? NS_BASE_STREAM_CLOSED : NS_OK;
}

NS_IMETHODIMP
FixedBufferOutputStream::Write(const char* aBuf, uint32_t aCount,
                               uint32_t* _retval) {
  return WriteSegments(NS_CopyBufferToSegment, const_cast<char*>(aBuf), aCount,
                       _retval);
}

NS_IMETHODIMP
FixedBufferOutputStream::WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
                                   uint32_t* _retval) {
  return WriteSegments(NS_CopyStreamToSegment, aFromStream, aCount, _retval);
}

NS_IMETHODIMP
FixedBufferOutputStream::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
                                       uint32_t aCount, uint32_t* _retval) {
  MOZ_ASSERT(_retval);

  MutexAutoLock autoLock(mMutex);

  if (mWriting) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  if (mClosed) {
    return NS_BASE_STREAM_CLOSED;
  }

  size_t length = mSink->Data().Length();
  size_t offset = mOffset;

  MOZ_ASSERT(length >= offset, "Bad stream state!");

  size_t maxCount = length - offset;
  if (maxCount == 0) {
    *_retval = 0;
    return NS_OK;
  }

  if (aCount > maxCount) {
    aCount = maxCount;
  }

  mWriting = true;

  nsresult rv;

  {
    MutexAutoUnlock autoUnlock(mMutex);

    rv = aReader(this, aClosure, mSink->Data().Elements() + offset, 0, aCount,
                 _retval);
  }

  mWriting = false;

  if (NS_SUCCEEDED(rv)) {
    MOZ_ASSERT(*_retval <= aCount,
               "Reader should not read more than we asked it to read!");
    mOffset += *_retval;
  }

  // As defined in nsIOutputStream.idl, do not pass reader func errors.
  return NS_OK;
}

NS_IMETHODIMP
FixedBufferOutputStream::IsNonBlocking(bool* _retval) {
  *_retval = true;
  return NS_OK;
}

}  // namespace mozilla