1/*
2 * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16#include <aws/core/utils/stream/ResponseStream.h>
17#include <aws/core/utils/memory/stl/AWSStringStream.h>
18
19#if defined(_GLIBCXX_FULLY_DYNAMIC_STRING) && _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && defined(__ANDROID__)
20#include <aws/core/utils/stream/SimpleStreamBuf.h>
21using DefaultStreamBufType = Aws::Utils::Stream::SimpleStreamBuf;
22#else
23using DefaultStreamBufType = Aws::StringBuf;
24#endif
25
26using namespace Aws::Utils::Stream;
27
28ResponseStream::ResponseStream(void) :
29 m_underlyingStream(nullptr)
30{
31}
32
33ResponseStream::ResponseStream(Aws::IOStream* underlyingStreamToManage) :
34 m_underlyingStream(underlyingStreamToManage)
35{
36}
37
38ResponseStream::ResponseStream(const Aws::IOStreamFactory& factory) :
39 m_underlyingStream(factory())
40{
41}
42
43ResponseStream::ResponseStream(ResponseStream&& toMove) : m_underlyingStream(toMove.m_underlyingStream)
44{
45 toMove.m_underlyingStream = nullptr;
46}
47
48ResponseStream& ResponseStream::operator=(ResponseStream&& toMove)
49{
50 if(m_underlyingStream == toMove.m_underlyingStream)
51 {
52 return *this;
53 }
54
55 ReleaseStream();
56 m_underlyingStream = toMove.m_underlyingStream;
57 toMove.m_underlyingStream = nullptr;
58
59 return *this;
60}
61
62ResponseStream::~ResponseStream()
63{
64 ReleaseStream();
65}
66
67void ResponseStream::ReleaseStream()
68{
69 if (m_underlyingStream)
70 {
71 m_underlyingStream->flush();
72 Aws::Delete(m_underlyingStream);
73 }
74
75 m_underlyingStream = nullptr;
76}
77
78static const char *DEFAULT_STREAM_TAG = "DefaultUnderlyingStream";
79
80DefaultUnderlyingStream::DefaultUnderlyingStream() :
81 Base( Aws::New< DefaultStreamBufType >( DEFAULT_STREAM_TAG ) )
82{}
83
84DefaultUnderlyingStream::DefaultUnderlyingStream(Aws::UniquePtr<std::streambuf> buf) :
85 Base(buf.release())
86{}
87
88DefaultUnderlyingStream::~DefaultUnderlyingStream()
89{
90 if( rdbuf() )
91 {
92 Aws::Delete( rdbuf() );
93 }
94}
95
96static const char* RESPONSE_STREAM_FACTORY_TAG = "ResponseStreamFactory";
97
98Aws::IOStream* Aws::Utils::Stream::DefaultResponseStreamFactoryMethod()
99{
100 return Aws::New<Aws::Utils::Stream::DefaultUnderlyingStream>(RESPONSE_STREAM_FACTORY_TAG);
101}
102