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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_recordreplay_BufferStream_h
8 #define mozilla_recordreplay_BufferStream_h
10 #include "InfallibleVector.h"
13 namespace recordreplay
{
15 // BufferStream provides similar functionality to Stream in File.h, allowing
16 // reading or writing to a stream of data backed by an in memory buffer instead
17 // of data stored on disk.
19 InfallibleVector
<char>* mOutput
;
25 BufferStream(const char* aInput
, size_t aInputSize
)
26 : mOutput(nullptr), mInput(aInput
), mInputSize(aInputSize
) {}
28 explicit BufferStream(InfallibleVector
<char>* aOutput
)
29 : mOutput(aOutput
), mInput(nullptr), mInputSize(0) {}
31 void WriteBytes(const void* aData
, size_t aSize
) {
32 MOZ_RELEASE_ASSERT(mOutput
);
33 mOutput
->append((char*)aData
, aSize
);
36 void WriteScalar(size_t aValue
) { WriteBytes(&aValue
, sizeof(aValue
)); }
38 void ReadBytes(void* aData
, size_t aSize
) {
40 MOZ_RELEASE_ASSERT(mInput
);
41 MOZ_RELEASE_ASSERT(aSize
<= mInputSize
);
42 memcpy(aData
, mInput
, aSize
);
50 ReadBytes(&rv
, sizeof(rv
));
55 MOZ_RELEASE_ASSERT(mInput
);
56 return mInputSize
== 0;
60 } // namespace recordreplay
61 } // namespace mozilla
63 #endif // mozilla_recordreplay_BufferStream_h