blob: 3ca518b686dd1b8b19710b5f90386a27577fe576 [file] [log] [blame]
Brian Carlstromc6dfdac2013-08-26 18:57:31 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
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 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "buffered_output_stream.h"
18
19#include <string.h>
20
21namespace art {
22
23BufferedOutputStream::BufferedOutputStream(OutputStream* out)
24 : OutputStream(out->GetLocation()), out_(out), used_(0) {}
25
Ian Rogersef7d42f2014-01-06 12:55:46 -080026bool BufferedOutputStream::WriteFully(const void* buffer, size_t byte_count) {
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070027 if (byte_count > kBufferSize) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000028 if (!Flush()) {
29 return false;
30 }
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070031 return out_->WriteFully(buffer, byte_count);
32 }
33 if (used_ + byte_count > kBufferSize) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000034 if (!Flush()) {
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070035 return false;
36 }
37 }
38 const uint8_t* src = reinterpret_cast<const uint8_t*>(buffer);
39 memcpy(&buffer_[used_], src, byte_count);
40 used_ += byte_count;
41 return true;
42}
43
44bool BufferedOutputStream::Flush() {
45 bool success = true;
46 if (used_ > 0) {
47 success = out_->WriteFully(&buffer_[0], used_);
48 used_ = 0;
49 }
50 return success;
51}
52
53off_t BufferedOutputStream::Seek(off_t offset, Whence whence) {
54 if (!Flush()) {
55 return -1;
56 }
57 return out_->Seek(offset, whence);
58}
59
60} // namespace art