| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "wtf/ArrayBuffer.h" | 34 #include "wtf/ArrayBuffer.h" |
| 35 #include "wtf/Noncopyable.h" | 35 #include "wtf/Noncopyable.h" |
| 36 #include "wtf/RefPtr.h" | 36 #include "wtf/RefPtr.h" |
| 37 #include "wtf/text/WTFString.h" | 37 #include "wtf/text/WTFString.h" |
| 38 | 38 |
| 39 namespace WTF { | 39 namespace WTF { |
| 40 | 40 |
| 41 // A utility class to build an ArrayBuffer instance. Validity must be checked | 41 // A utility class to build an ArrayBuffer instance. Validity must be checked |
| 42 // by isValid() before using an instance. | 42 // by isValid() before using an instance. |
| 43 class WTF_EXPORT ArrayBufferBuilder { | 43 class WTF_EXPORT ArrayBufferBuilder { |
| 44 // Disallow copying since it's expensive and we don't want code to do it by | 44 // Disallow copying since it's expensive and we don't want code to do it by |
| 45 // accident. | 45 // accident. |
| 46 WTF_MAKE_NONCOPYABLE(ArrayBufferBuilder); | 46 WTF_MAKE_NONCOPYABLE(ArrayBufferBuilder); |
| 47 | 47 |
| 48 public: | 48 public: |
| 49 // Creates an ArrayBufferBuilder using the default capacity. | 49 // Creates an ArrayBufferBuilder using the default capacity. |
| 50 ArrayBufferBuilder(); | 50 ArrayBufferBuilder(); |
| 51 | 51 |
| 52 ArrayBufferBuilder(unsigned capacity) | 52 ArrayBufferBuilder(unsigned capacity) |
| 53 : m_bytesUsed(0) | 53 : m_bytesUsed(0), m_variableCapacity(true) { |
| 54 , m_variableCapacity(true) | 54 m_buffer = ArrayBuffer::create(capacity, 1); |
| 55 { | 55 } |
| 56 m_buffer = ArrayBuffer::create(capacity, 1); | |
| 57 } | |
| 58 | 56 |
| 59 bool isValid() const | 57 bool isValid() const { |
| 60 { | 58 return m_buffer; |
| 61 return m_buffer; | 59 } |
| 62 } | |
| 63 | 60 |
| 64 // Appending empty data is not allowed. | 61 // Appending empty data is not allowed. |
| 65 unsigned append(const char* data, unsigned length); | 62 unsigned append(const char* data, unsigned length); |
| 66 | 63 |
| 67 // Returns the accumulated data as an ArrayBuffer instance. If needed, | 64 // Returns the accumulated data as an ArrayBuffer instance. If needed, |
| 68 // creates a new ArrayBuffer instance and copies contents from the internal | 65 // creates a new ArrayBuffer instance and copies contents from the internal |
| 69 // buffer to it. Otherwise, returns a PassRefPtr pointing to the internal | 66 // buffer to it. Otherwise, returns a PassRefPtr pointing to the internal |
| 70 // buffer. | 67 // buffer. |
| 71 PassRefPtr<ArrayBuffer> toArrayBuffer(); | 68 PassRefPtr<ArrayBuffer> toArrayBuffer(); |
| 72 | 69 |
| 73 // Converts the accumulated data into a String using the default encoding. | 70 // Converts the accumulated data into a String using the default encoding. |
| 74 String toString(); | 71 String toString(); |
| 75 | 72 |
| 76 // Number of bytes currently accumulated. | 73 // Number of bytes currently accumulated. |
| 77 unsigned byteLength() const | 74 unsigned byteLength() const { |
| 78 { | 75 return m_bytesUsed; |
| 79 return m_bytesUsed; | 76 } |
| 80 } | |
| 81 | 77 |
| 82 // Number of bytes allocated. | 78 // Number of bytes allocated. |
| 83 unsigned capacity() const | 79 unsigned capacity() const { |
| 84 { | 80 return m_buffer->byteLength(); |
| 85 return m_buffer->byteLength(); | 81 } |
| 86 } | |
| 87 | 82 |
| 88 void shrinkToFit(); | 83 void shrinkToFit(); |
| 89 | 84 |
| 90 const void* data() const | 85 const void* data() const { |
| 91 { | 86 return m_buffer->data(); |
| 92 return m_buffer->data(); | 87 } |
| 93 } | |
| 94 | 88 |
| 95 // If set to false, the capacity won't be expanded and when appended data | 89 // If set to false, the capacity won't be expanded and when appended data |
| 96 // overflows, the overflowed part will be dropped. | 90 // overflows, the overflowed part will be dropped. |
| 97 void setVariableCapacity(bool value) | 91 void setVariableCapacity(bool value) { |
| 98 { | 92 m_variableCapacity = value; |
| 99 m_variableCapacity = value; | 93 } |
| 100 } | |
| 101 | 94 |
| 102 private: | 95 private: |
| 103 // Expands the size of m_buffer to size + m_bytesUsed bytes. Returns true | 96 // Expands the size of m_buffer to size + m_bytesUsed bytes. Returns true |
| 104 // iff successful. If reallocation is needed, copies only data in | 97 // iff successful. If reallocation is needed, copies only data in |
| 105 // [0, m_bytesUsed) range. | 98 // [0, m_bytesUsed) range. |
| 106 bool expandCapacity(unsigned size); | 99 bool expandCapacity(unsigned size); |
| 107 | 100 |
| 108 unsigned m_bytesUsed; | 101 unsigned m_bytesUsed; |
| 109 bool m_variableCapacity; | 102 bool m_variableCapacity; |
| 110 RefPtr<ArrayBuffer> m_buffer; | 103 RefPtr<ArrayBuffer> m_buffer; |
| 111 }; | 104 }; |
| 112 | 105 |
| 113 } // namespace WTF | 106 } // namespace WTF |
| 114 | 107 |
| 115 using WTF::ArrayBufferBuilder; | 108 using WTF::ArrayBufferBuilder; |
| 116 | 109 |
| 117 #endif // ArrayBufferBuilder_h | 110 #endif // ArrayBufferBuilder_h |
| OLD | NEW |