Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
| 17 | #include "file.h" |
| 18 | |
| 19 | namespace art { |
| 20 | |
| 21 | bool File::ReadFully(void* buffer, int64_t num_bytes) { |
| 22 | int64_t remaining = num_bytes; |
| 23 | char* current_buffer = reinterpret_cast<char*>(buffer); |
| 24 | while (remaining > 0) { |
| 25 | int bytes_read = Read(current_buffer, remaining); |
| 26 | if (bytes_read <= 0) { |
| 27 | return false; |
| 28 | } |
| 29 | remaining -= bytes_read; // Reduce the number of remaining bytes. |
| 30 | current_buffer += bytes_read; // Move the buffer forward. |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | bool File::WriteFully(const void* buffer, int64_t num_bytes) { |
| 37 | int64_t remaining = num_bytes; |
| 38 | const char* current_buffer = reinterpret_cast<const char*>(buffer); |
| 39 | while (remaining > 0) { |
| 40 | int bytes_read = Write(current_buffer, remaining); |
| 41 | if (bytes_read < 0) { |
| 42 | return false; |
| 43 | } |
| 44 | remaining -= bytes_read; // Reduce the number of remaining bytes. |
| 45 | current_buffer += bytes_read; // Move the buffer forward. |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | } // namespace art |