|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2017 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + ******************************************************************************/ |
| 16 | +package com.ibm.javametrics.agent; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | + |
| 20 | +public class JsonDataBucket implements Bucket { |
| 21 | + |
| 22 | + private static final int MAX_SIZE = 4 * 1024; |
| 23 | + private ArrayList<String> bucket = new ArrayList<String>(100); |
| 24 | + int size = 0; |
| 25 | + int cursor = 0; |
| 26 | + |
| 27 | + @Override |
| 28 | + public int getSize() { |
| 29 | + return size; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void empty() { |
| 34 | + synchronized (bucket) { |
| 35 | + bucket.clear(); |
| 36 | + cursor = 0; |
| 37 | + size = 0; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void pushData(String data) { |
| 43 | + synchronized (bucket) { |
| 44 | + bucket.add(data); |
| 45 | + size += data.length(); |
| 46 | + |
| 47 | + System.err.println("pushed " + cursor + " " + bucket.size() + " " + size); |
| 48 | + while (size > MAX_SIZE && cursor > 0) { |
| 49 | + String removed = bucket.remove(0); |
| 50 | + cursor--; |
| 51 | + size -= removed.length(); |
| 52 | + System.err.println("trimmed " + cursor + " " + bucket.size() + " " + size); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getNext() { |
| 59 | + String data = null; |
| 60 | + synchronized (bucket) { |
| 61 | + if (cursor < bucket.size()) { |
| 62 | + data = bucket.get(cursor); |
| 63 | + cursor++; |
| 64 | + System.err.println("returned " + cursor + " " + bucket.size() + " " + size); |
| 65 | + } |
| 66 | + } |
| 67 | + return data; |
| 68 | + } |
| 69 | +} |
0 commit comments