1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright (C) 2013 Google Inc. All rights reserved.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Redistribution and use in source and binary forms, with or without
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * modification, are permitted provided that the following conditions are
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * met:
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
8807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com *     * Redistributions of source code must retain the above copyright
9807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com * notice, this list of conditions and the following disclaimer.
10807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com *     * Redistributions in binary form must reproduce the above
11807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com * copyright notice, this list of conditions and the following disclaimer
12807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com * in the documentation and/or other materials provided with the
13807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com * distribution.
14807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com *     * Neither the name of Google Inc. nor the names of its
15807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com * contributors may be used to endorse or promote products derived from
16a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com * this software without specific prior written permission.
17a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com *
18a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2254bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2550a69a0727076d6590a23dd0f6501371573c7d28robertphillips@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2654bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com */
30807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
31807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com#ifndef ArrayBufferBuilder_h
32a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org#define ArrayBufferBuilder_h
33a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org
34a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org#include "wtf/ArrayBuffer.h"
35a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org#include "wtf/Noncopyable.h"
36807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com#include "wtf/RefPtr.h"
37807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com#include "wtf/text/WTFString.h"
38a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com
39d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.comnamespace WTF {
4050a69a0727076d6590a23dd0f6501371573c7d28robertphillips@google.com
4154bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com// A utility class to build an ArrayBuffer instance. Validity must be checked
42a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com// by isValid() before using an instance.
43807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.comclass WTF_EXPORT ArrayBufferBuilder {
44807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // Disallow copying since it's expensive and we don't want code to do it by
45f539318f0d3dba743ec1886d5d9df0fb1be628a1tfarina    // accident.
46807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    WTF_MAKE_NONCOPYABLE(ArrayBufferBuilder);
47807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
48807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.compublic:
49807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // Creates an ArrayBufferBuilder using the default capacity.
50807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    ArrayBufferBuilder();
51807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
52807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    ArrayBufferBuilder(unsigned capacity)
53807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        : m_bytesUsed(0)
54807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        , m_variableCapacity(true)
55807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    {
56807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        m_buffer = ArrayBuffer::create(capacity, 1);
57807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    }
58807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
59807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    bool isValid() const
60807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    {
61807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        return m_buffer;
62807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    }
63807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
64807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // Appending empty data is not allowed.
65807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    unsigned append(const char* data, unsigned length);
66a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com
67807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // Returns the accumulated data as an ArrayBuffer instance. If needed,
68807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // creates a new ArrayBuffer instance and copies contents from the internal
69807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // buffer to it. Otherwise, returns a PassRefPtr pointing to the internal
70807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // buffer.
71807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    PassRefPtr<ArrayBuffer> toArrayBuffer();
72807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
73807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // Converts the accumulated data into a String using the default encoding.
74807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    String toString();
75807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
76807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // Number of bytes currently accumulated.
77807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    unsigned byteLength() const
78807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    {
79807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        return m_bytesUsed;
80807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    }
81807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
82807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // Number of bytes allocated.
83807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    unsigned capacity() const
84807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    {
85807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        return m_buffer->byteLength();
865fb6bd4b7e8d00b7f2543ca10ec9022b32632f29caryclark    }
87a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com
88807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    void shrinkToFit();
89807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
90807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    const void* data() const
91807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    {
92807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        return m_buffer->data();
93807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    }
94807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
95807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // If set to false, the capacity won't be expanded and when appended data
96807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    // overflows, the overflowed part will be dropped.
97807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    void setVariableCapacity(bool value)
98807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com    {
99807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com        m_variableCapacity = value;
100a8f60f25022d64ba29cfd7c4465151f431d0a264reed@google.com    }
101807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.com
102807cec4397cb50a64de7355ae129cc7c8f2c772bbsalomon@google.comprivate:
10354bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com    // Expands the size of m_buffer to size + m_bytesUsed bytes. Returns true
10454bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com    // iff successful. If reallocation is needed, copies only data in
10554bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com    // [0, m_bytesUsed) range.
10654bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com    bool expandCapacity(unsigned size);
10754bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com
1084b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    unsigned m_bytesUsed;
1094b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    bool m_variableCapacity;
1104b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    RefPtr<ArrayBuffer> m_buffer;
1114b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org};
11254bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com
11354bb7abc09e06b41aaf3dca633d27ccc7b291badrobertphillips@google.com} // namespace WTF
11450a69a0727076d6590a23dd0f6501371573c7d28robertphillips@google.com
11550a69a0727076d6590a23dd0f6501371573c7d28robertphillips@google.comusing WTF::ArrayBufferBuilder;
11650a69a0727076d6590a23dd0f6501371573c7d28robertphillips@google.com
11750a69a0727076d6590a23dd0f6501371573c7d28robertphillips@google.com#endif // ArrayBufferBuilder_h
11850a69a0727076d6590a23dd0f6501371573c7d28robertphillips@google.com