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