1/*
2Copyright (c) 2011 Stanislav Vitvitskiy
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this
5software and associated documentation files (the "Software"), to deal in the Software
6without restriction, including without limitation the rights to use, copy, modify,
7merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8permit persons to whom the Software is furnished to do so, subject to the following
9conditions:
10
11The above copyright notice and this permission notice shall be included in all copies or
12substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19OR OTHER DEALINGS IN THE SOFTWARE.
20*/
21package com.googlecode.mp4parser.h264;
22
23public class CharCache {
24    private char[] cache;
25    private int pos;
26
27    public CharCache(int capacity) {
28        cache = new char[capacity];
29    }
30
31    public void append(String str) {
32        char[] chars = str.toCharArray();
33        int available = cache.length - pos;
34        int toWrite = chars.length < available ? chars.length : available;
35        System.arraycopy(chars, 0, cache, pos, toWrite);
36        pos += toWrite;
37    }
38
39    public String toString() {
40        return new String(cache, 0, pos);
41    }
42
43    public void clear() {
44        pos = 0;
45    }
46
47    public void append(char c) {
48        if (pos < cache.length - 1) {
49            cache[pos] = c;
50            pos++;
51        }
52    }
53
54    public int length() {
55        return pos;
56    }
57}
58