1/*
2 * Copyright 2014 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#ifndef IMG_UTILS_BYTE_ARRAY_OUTPUT_H
18#define IMG_UTILS_BYTE_ARRAY_OUTPUT_H
19
20#include <img_utils/Output.h>
21
22#include <utils/Errors.h>
23#include <utils/Vector.h>
24
25#include <cutils/compiler.h>
26#include <stdint.h>
27
28namespace android {
29namespace img_utils {
30
31/**
32 * Utility class that accumulates written bytes into a buffer.
33 */
34class ANDROID_API ByteArrayOutput : public Output {
35    public:
36
37        ByteArrayOutput();
38
39        virtual ~ByteArrayOutput();
40
41        /**
42         * Open this ByteArrayOutput.
43         *
44         * Returns OK on success, or a negative error code.
45         */
46        virtual status_t open();
47
48        /**
49         * Write bytes from the given buffer.  The number of bytes given in the count
50         * argument will be written.  Bytes will be written from the given buffer starting
51         * at the index given in the offset argument.
52         *
53         * Returns OK on success, or a negative error code.
54         */
55        virtual status_t write(const uint8_t* buf, size_t offset, size_t count);
56
57        /**
58         * Close this ByteArrayOutput.
59         *
60         * Returns OK on success, or a negative error code.
61         */
62        virtual status_t close();
63
64        /**
65         * Get current size of the array of bytes written.
66         */
67        virtual size_t getSize() const;
68
69        /**
70         * Get pointer to array of bytes written.  It is not valid to use this pointer if
71         * open, write, or close is called after this method.
72         */
73        virtual const uint8_t* getArray() const;
74
75    protected:
76        Vector<uint8_t> mByteArray;
77};
78
79} /*namespace img_utils*/
80} /*namespace android*/
81
82#endif /*IMG_UTILS_BYTE_ARRAY_OUTPUT_H*/
83