1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 package org.mozilla.gecko.sync.repositories.uploaders;
6 
7 import android.support.annotation.CheckResult;
8 
9 import java.util.ArrayList;
10 
11 /**
12  * Owns per-payload record byte and recordGuid buffers.
13  */
14 /* @ThreadSafe */
15 public class Payload extends BufferSizeTracker {
16     // Data of outbound records.
17     /* @GuardedBy("accessLock") */ private final ArrayList<byte[]> recordsBuffer = new ArrayList<>();
18 
19     // GUIDs of outbound records. Used to fail entire payloads.
20     /* @GuardedBy("accessLock") */ private final ArrayList<String> recordGuidsBuffer = new ArrayList<>();
21 
Payload(Object payloadLock, long maxBytes, long maxRecords)22     public Payload(Object payloadLock, long maxBytes, long maxRecords) {
23         super(payloadLock, maxBytes, maxRecords);
24     }
25 
26     @Override
addAndEstimateIfFull(long recordDelta)27     protected boolean addAndEstimateIfFull(long recordDelta) {
28         throw new UnsupportedOperationException();
29     }
30 
31     @CheckResult
addAndEstimateIfFull(long recordDelta, byte[] recordBytes, String guid)32     protected boolean addAndEstimateIfFull(long recordDelta, byte[] recordBytes, String guid) {
33         synchronized (accessLock) {
34             recordsBuffer.add(recordBytes);
35             recordGuidsBuffer.add(guid);
36             return super.addAndEstimateIfFull(recordDelta);
37         }
38     }
39 
40     @Override
reset()41     protected void reset() {
42         synchronized (accessLock) {
43             super.reset();
44             recordsBuffer.clear();
45             recordGuidsBuffer.clear();
46         }
47     }
48 
getRecordsBuffer()49     protected ArrayList<byte[]> getRecordsBuffer() {
50         synchronized (accessLock) {
51             return new ArrayList<>(recordsBuffer);
52         }
53     }
54 
getRecordGuidsBuffer()55     protected ArrayList<String> getRecordGuidsBuffer() {
56         synchronized (accessLock) {
57             return new ArrayList<>(recordGuidsBuffer);
58         }
59     }
60 
isEmpty()61     protected boolean isEmpty() {
62         synchronized (accessLock) {
63             return recordsBuffer.isEmpty();
64         }
65     }
66 }