Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace art { |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 22 | namespace linker { |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 23 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 24 | BufferedOutputStream::BufferedOutputStream(std::unique_ptr<OutputStream> out) |
| 25 | : OutputStream(out->GetLocation()), // Before out is moved to out_. |
| 26 | out_(std::move(out)), |
| 27 | used_(0) {} |
| 28 | |
| 29 | BufferedOutputStream::~BufferedOutputStream() { |
| 30 | FlushBuffer(); |
| 31 | } |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 32 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 33 | bool BufferedOutputStream::WriteFully(const void* buffer, size_t byte_count) { |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 34 | if (byte_count > kBufferSize) { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 35 | if (!FlushBuffer()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 36 | return false; |
| 37 | } |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 38 | return out_->WriteFully(buffer, byte_count); |
| 39 | } |
| 40 | if (used_ + byte_count > kBufferSize) { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 41 | if (!FlushBuffer()) { |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | const uint8_t* src = reinterpret_cast<const uint8_t*>(buffer); |
| 46 | memcpy(&buffer_[used_], src, byte_count); |
| 47 | used_ += byte_count; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool BufferedOutputStream::Flush() { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 52 | return FlushBuffer() && out_->Flush(); |
| 53 | } |
| 54 | |
| 55 | bool BufferedOutputStream::FlushBuffer() { |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 56 | bool success = true; |
| 57 | if (used_ > 0) { |
| 58 | success = out_->WriteFully(&buffer_[0], used_); |
| 59 | used_ = 0; |
| 60 | } |
| 61 | return success; |
| 62 | } |
| 63 | |
| 64 | off_t BufferedOutputStream::Seek(off_t offset, Whence whence) { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 65 | if (!FlushBuffer()) { |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | return out_->Seek(offset, whence); |
| 69 | } |
| 70 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 71 | } // namespace linker |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 72 | } // namespace art |