Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/quota/MemoryOutputStream.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "MemoryOutputStream.h"
8
9
#include "nsStreamUtils.h"
10
11
namespace mozilla {
12
namespace dom {
13
namespace quota {
14
15
// static
16
already_AddRefed<MemoryOutputStream>
17
MemoryOutputStream::Create(uint64_t aSize)
18
0
{
19
0
  MOZ_ASSERT(aSize, "Passed zero size!");
20
0
21
0
  if (NS_WARN_IF(aSize > UINT32_MAX)) {
22
0
    return nullptr;
23
0
  }
24
0
25
0
  RefPtr<MemoryOutputStream> stream = new MemoryOutputStream();
26
0
27
0
  if (NS_WARN_IF(!stream->mData.SetLength(aSize, fallible))) {
28
0
    return nullptr;
29
0
  }
30
0
31
0
  return stream.forget();
32
0
}
33
34
NS_IMPL_ISUPPORTS(MemoryOutputStream, nsIOutputStream)
35
36
NS_IMETHODIMP
37
MemoryOutputStream::Close()
38
0
{
39
0
  mData.Truncate(mOffset);
40
0
  return NS_OK;
41
0
}
42
43
NS_IMETHODIMP
44
MemoryOutputStream::Write(const char* aBuf, uint32_t aCount, uint32_t* _retval)
45
0
{
46
0
  return WriteSegments(NS_CopySegmentToBuffer, (char*)aBuf, aCount, _retval);
47
0
}
48
49
NS_IMETHODIMP
50
MemoryOutputStream::Flush()
51
0
{
52
0
  return NS_OK;
53
0
}
54
55
NS_IMETHODIMP
56
MemoryOutputStream::WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
57
                              uint32_t* _retval)
58
0
{
59
0
  return NS_ERROR_NOT_IMPLEMENTED;
60
0
}
61
62
NS_IMETHODIMP
63
MemoryOutputStream::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
64
                                  uint32_t aCount, uint32_t* _retval)
65
0
{
66
0
  MOZ_ASSERT(mData.Length() >= mOffset, "Bad stream state!");
67
0
68
0
  uint32_t maxCount = mData.Length() - mOffset;
69
0
  if (maxCount == 0) {
70
0
    *_retval = 0;
71
0
    return NS_OK;
72
0
  }
73
0
74
0
  if (aCount > maxCount) {
75
0
    aCount = maxCount;
76
0
  }
77
0
78
0
  nsresult rv = aReader(this, aClosure, mData.BeginWriting() + mOffset, 0,
79
0
                        aCount, _retval);
80
0
  if (NS_SUCCEEDED(rv)) {
81
0
    MOZ_ASSERT(*_retval <= aCount,
82
0
               "Reader should not read more than we asked it to read!");
83
0
    mOffset += *_retval;
84
0
  }
85
0
86
0
  return NS_OK;
87
0
}
88
89
NS_IMETHODIMP
90
MemoryOutputStream::IsNonBlocking(bool* _retval)
91
0
{
92
0
  *_retval = false;
93
0
  return NS_OK;
94
0
}
95
96
} // namespace quota
97
} // namespace dom
98
} // namespace mozilla