Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright (C) 2011 The sfntly 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 SFNTLY_CPP_SRC_SFNTLY_DATA_BYTE_ARRAY_H_
     18 #define SFNTLY_CPP_SRC_SFNTLY_DATA_BYTE_ARRAY_H_
     19 
     20 #include "sfntly/port/refcount.h"
     21 #include "sfntly/port/type.h"
     22 #include "sfntly/port/input_stream.h"
     23 #include "sfntly/port/output_stream.h"
     24 
     25 namespace sfntly {
     26 
     27 // An abstraction to a contiguous array of bytes.
     28 // C++ port of this class assumes that the data are stored in a linear region
     29 // like std::vector.
     30 class ByteArray : virtual public RefCount {
     31  public:
     32   virtual ~ByteArray();
     33 
     34   // Gets the current filled and readable length of the array.
     35   int32_t Length();
     36 
     37   // Gets the maximum size of the array. This is the maximum number of bytes that
     38   // the array can hold and all of it may not be filled with data or even fully
     39   // allocated yet.
     40   int32_t Size();
     41 
     42   // Determines whether or not this array is growable or of fixed size.
     43   bool growable() { return growable_; }
     44 
     45   int32_t SetFilledLength(int32_t filled_length);
     46 
     47   // Gets the byte from the given index.
     48   // @param index the index into the byte array
     49   // @return the byte or -1 if reading beyond the bounds of the data
     50   virtual int32_t Get(int32_t index);
     51 
     52   // Gets the bytes from the given index and fill the buffer with them. As many
     53   // bytes as will fit into the buffer are read unless that would go past the
     54   // end of the array.
     55   // @param index the index into the byte array
     56   // @param b the buffer to put the bytes read into
     57   // @return the number of bytes read from the buffer
     58   virtual int32_t Get(int32_t index, ByteVector* b);
     59 
     60   // Gets the bytes from the given index and fill the buffer with them starting
     61   // at the offset given. As many bytes as the specified length are read unless
     62   // that would go past the end of the array.
     63   // @param index the index into the byte array
     64   // @param b the buffer to put the bytes read into
     65   // @param offset the location in the buffer to start putting the bytes
     66   // @param length the number of bytes to put into the buffer
     67   // @return the number of bytes read from the buffer
     68   virtual int32_t Get(int32_t index,
     69                       byte_t* b,
     70                       int32_t offset,
     71                       int32_t length);
     72 
     73   // Puts the specified byte into the array at the given index unless that would
     74   // be beyond the length of the array and it isn't growable.
     75   virtual void Put(int32_t index, byte_t b);
     76 
     77   // Puts the specified bytes into the array at the given index. The entire
     78   // buffer is put into the array unless that would extend beyond the length and
     79   // the array isn't growable.
     80   virtual int32_t Put(int32_t index, ByteVector* b);
     81 
     82   // Puts the specified bytes into the array at the given index. All of the bytes
     83   // specified are put into the array unless that would extend beyond the length
     84   // and the array isn't growable. The bytes to be put into the array are those
     85   // in the buffer from the given offset and for the given length.
     86   // @param index the index into the ByteArray
     87   // @param b the bytes to put into the array
     88   // @param offset the offset in the bytes to start copying from
     89   // @param length the number of bytes to copy into the array
     90   // @return the number of bytes actually written
     91   virtual int32_t Put(int32_t index,
     92                       byte_t* b,
     93                       int32_t offset,
     94                       int32_t length);
     95 
     96   // Fully copies this ByteArray to another ByteArray to the extent that the
     97   // destination array has storage for the data copied.
     98   virtual int32_t CopyTo(ByteArray* array);
     99 
    100   // Copies a segment of this ByteArray to another ByteArray.
    101   // @param array the destination
    102   // @param offset the offset in this ByteArray to start copying from
    103   // @param length the maximum length in bytes to copy
    104   // @return the number of bytes copied
    105   virtual int32_t CopyTo(ByteArray* array, int32_t offset, int32_t length);
    106 
    107   // Copies this ByteArray to another ByteArray.
    108   // @param dstOffset the offset in the destination array to start copying to
    109   // @param array the destination
    110   // @param srcOffset the offset in this ByteArray to start copying from
    111   // @param length the maximum length in bytes to copy
    112   // @return the number of bytes copied
    113   virtual int32_t CopyTo(int32_t dst_offset,
    114                          ByteArray* array,
    115                          int32_t src_offset,
    116                          int32_t length);
    117 
    118   // Copies this ByteArray to an OutputStream.
    119   // @param os the destination
    120   // @return the number of bytes copied
    121   virtual int32_t CopyTo(OutputStream* os);
    122 
    123   // Copies this ByteArray to an OutputStream.
    124   // @param os the destination
    125   // @param offset
    126   // @param length
    127   // @return the number of bytes copied
    128   virtual int32_t CopyTo(OutputStream* os, int32_t offset, int32_t length);
    129 
    130   // Copies from the InputStream into this ByteArray.
    131   // @param is the source
    132   // @param length the number of bytes to copy
    133   virtual bool CopyFrom(InputStream* is, int32_t length);
    134 
    135   // Copies everything from the InputStream into this ByteArray.
    136   // @param is the source
    137   virtual bool CopyFrom(InputStream* is);
    138 
    139  protected:
    140   // filledLength the length that is "filled" and readable counting from offset.
    141   // storageLength the maximum storage size of the underlying data.
    142   // growable is the storage growable - storageLength is the max growable size.
    143   ByteArray(int32_t filled_length, int32_t storage_length, bool growable);
    144   ByteArray(int32_t filled_length, int32_t storage_length);
    145   void Init(int32_t filled_length, int32_t storage_length, bool growable);
    146 
    147   // Internal subclass API
    148 
    149   // Stores the byte at the index given.
    150   // @param index the location to store at
    151   // @param b the byte to store
    152   virtual void InternalPut(int32_t index, byte_t b) = 0;
    153 
    154   // Stores the array of bytes at the given index.
    155   // @param index the location to store at
    156   // @param b the bytes to store
    157   // @param offset the offset to start from in the byte array
    158   // @param length the length of the byte array to store from the offset
    159   // @return the number of bytes actually stored
    160   virtual int32_t InternalPut(int32_t index,
    161                               byte_t* b,
    162                               int32_t offset,
    163                               int32_t length) = 0;
    164 
    165   // Gets the byte at the index given.
    166   // @param index the location to get from
    167   // @return the byte stored at the index
    168   virtual byte_t InternalGet(int32_t index) = 0;
    169 
    170   // Gets the bytes at the index given of the given length.
    171   // @param index the location to start getting from
    172   // @param b the array to put the bytes into
    173   // @param offset the offset in the array to put the bytes into
    174   // @param length the length of bytes to read
    175   // @return the number of bytes actually ready
    176   virtual int32_t InternalGet(int32_t index,
    177                               byte_t* b,
    178                               int32_t offset,
    179                               int32_t length) = 0;
    180 
    181   // Close this instance of the ByteArray.
    182   virtual void Close() = 0;
    183 
    184   // C++ port only, raw pointer to the first element of storage.
    185   virtual byte_t* Begin() = 0;
    186 
    187   // Java toString() not ported.
    188 
    189   static const int32_t COPY_BUFFER_SIZE;
    190 
    191  private:
    192   //bool bound_;  // unused, comment out
    193   int32_t filled_length_;
    194   int32_t storage_length_;
    195   bool growable_;
    196 };
    197 typedef Ptr<ByteArray> ByteArrayPtr;
    198 
    199 }  // namespace sfntly
    200 
    201 #endif  // SFNTLY_CPP_SRC_SFNTLY_DATA_BYTE_ARRAY_H_
    202