2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import static android.net.NetworkStats.IFACE_ALL;
20 import static android.net.NetworkStats.SET_DEFAULT;
21 import static android.net.NetworkStats.TAG_NONE;
22 import static android.net.NetworkStats.UID_ALL;
23 import static android.net.NetworkStatsHistory.DataStreamUtils.readFullLongArray;
24 import static android.net.NetworkStatsHistory.DataStreamUtils.readVarLongArray;
25 import static android.net.NetworkStatsHistory.DataStreamUtils.writeVarLongArray;
26 import static android.net.NetworkStatsHistory.Entry.UNKNOWN;
27 import static android.net.NetworkStatsHistory.ParcelUtils.readLongArray;
28 import static android.net.NetworkStatsHistory.ParcelUtils.writeLongArray;
29 import static android.text.format.DateUtils.SECOND_IN_MILLIS;
30 import static com.android.internal.util.ArrayUtils.total;
32 import android.os.Parcel;
33 import android.os.Parcelable;
34 import android.util.MathUtils;
36 import com.android.internal.util.IndentingPrintWriter;
38 import java.io.CharArrayWriter;
39 import java.io.DataInputStream;
40 import java.io.DataOutputStream;
41 import java.io.IOException;
42 import java.io.PrintWriter;
43 import java.net.ProtocolException;
44 import java.util.Arrays;
45 import java.util.Random;
48 * Collection of historical network statistics, recorded into equally-sized
49 * "buckets" in time. Internally it stores data in {@code long} series for more
50 * efficient persistence.
52 * Each bucket is defined by a {@link #bucketStart} timestamp, and lasts for
53 * {@link #bucketDuration}. Internally assumes that {@link #bucketStart} is
54 * sorted at all times.
58 public class NetworkStatsHistory implements Parcelable {
59 private static final int VERSION_INIT = 1;
60 private static final int VERSION_ADD_PACKETS = 2;
61 private static final int VERSION_ADD_ACTIVE = 3;
63 public static final int FIELD_ACTIVE_TIME = 0x01;
64 public static final int FIELD_RX_BYTES = 0x02;
65 public static final int FIELD_RX_PACKETS = 0x04;
66 public static final int FIELD_TX_BYTES = 0x08;
67 public static final int FIELD_TX_PACKETS = 0x10;
68 public static final int FIELD_OPERATIONS = 0x20;
70 public static final int FIELD_ALL = 0xFFFFFFFF;
72 private long bucketDuration;
73 private int bucketCount;
74 private long[] bucketStart;
75 private long[] activeTime;
76 private long[] rxBytes;
77 private long[] rxPackets;
78 private long[] txBytes;
79 private long[] txPackets;
80 private long[] operations;
81 private long totalBytes;
83 public static class Entry {
84 public static final long UNKNOWN = -1;
86 public long bucketDuration;
87 public long bucketStart;
88 public long activeTime;
90 public long rxPackets;
92 public long txPackets;
93 public long operations;
96 public NetworkStatsHistory(long bucketDuration) {
97 this(bucketDuration, 10, FIELD_ALL);
100 public NetworkStatsHistory(long bucketDuration, int initialSize) {
101 this(bucketDuration, initialSize, FIELD_ALL);
104 public NetworkStatsHistory(long bucketDuration, int initialSize, int fields) {
105 this.bucketDuration = bucketDuration;
106 bucketStart = new long[initialSize];
107 if ((fields & FIELD_ACTIVE_TIME) != 0) activeTime = new long[initialSize];
108 if ((fields & FIELD_RX_BYTES) != 0) rxBytes = new long[initialSize];
109 if ((fields & FIELD_RX_PACKETS) != 0) rxPackets = new long[initialSize];
110 if ((fields & FIELD_TX_BYTES) != 0) txBytes = new long[initialSize];
111 if ((fields & FIELD_TX_PACKETS) != 0) txPackets = new long[initialSize];
112 if ((fields & FIELD_OPERATIONS) != 0) operations = new long[initialSize];
117 public NetworkStatsHistory(NetworkStatsHistory existing, long bucketDuration) {
118 this(bucketDuration, existing.estimateResizeBuckets(bucketDuration));
119 recordEntireHistory(existing);
122 public NetworkStatsHistory(Parcel in) {
123 bucketDuration = in.readLong();
124 bucketStart = readLongArray(in);
125 activeTime = readLongArray(in);
126 rxBytes = readLongArray(in);
127 rxPackets = readLongArray(in);
128 txBytes = readLongArray(in);
129 txPackets = readLongArray(in);
130 operations = readLongArray(in);
131 bucketCount = bucketStart.length;
132 totalBytes = in.readLong();
136 public void writeToParcel(Parcel out, int flags) {
137 out.writeLong(bucketDuration);
138 writeLongArray(out, bucketStart, bucketCount);
139 writeLongArray(out, activeTime, bucketCount);
140 writeLongArray(out, rxBytes, bucketCount);
141 writeLongArray(out, rxPackets, bucketCount);
142 writeLongArray(out, txBytes, bucketCount);
143 writeLongArray(out, txPackets, bucketCount);
144 writeLongArray(out, operations, bucketCount);
145 out.writeLong(totalBytes);
148 public NetworkStatsHistory(DataInputStream in) throws IOException {
149 final int version = in.readInt();
152 bucketDuration = in.readLong();
153 bucketStart = readFullLongArray(in);
154 rxBytes = readFullLongArray(in);
155 rxPackets = new long[bucketStart.length];
156 txBytes = readFullLongArray(in);
157 txPackets = new long[bucketStart.length];
158 operations = new long[bucketStart.length];
159 bucketCount = bucketStart.length;
160 totalBytes = total(rxBytes) + total(txBytes);
163 case VERSION_ADD_PACKETS:
164 case VERSION_ADD_ACTIVE: {
165 bucketDuration = in.readLong();
166 bucketStart = readVarLongArray(in);
167 activeTime = (version >= VERSION_ADD_ACTIVE) ? readVarLongArray(in)
168 : new long[bucketStart.length];
169 rxBytes = readVarLongArray(in);
170 rxPackets = readVarLongArray(in);
171 txBytes = readVarLongArray(in);
172 txPackets = readVarLongArray(in);
173 operations = readVarLongArray(in);
174 bucketCount = bucketStart.length;
175 totalBytes = total(rxBytes) + total(txBytes);
179 throw new ProtocolException("unexpected version: " + version);
183 if (bucketStart.length != bucketCount || rxBytes.length != bucketCount
184 || rxPackets.length != bucketCount || txBytes.length != bucketCount
185 || txPackets.length != bucketCount || operations.length != bucketCount) {
186 throw new ProtocolException("Mismatched history lengths");
190 public void writeToStream(DataOutputStream out) throws IOException {
191 out.writeInt(VERSION_ADD_ACTIVE);
192 out.writeLong(bucketDuration);
193 writeVarLongArray(out, bucketStart, bucketCount);
194 writeVarLongArray(out, activeTime, bucketCount);
195 writeVarLongArray(out, rxBytes, bucketCount);
196 writeVarLongArray(out, rxPackets, bucketCount);
197 writeVarLongArray(out, txBytes, bucketCount);
198 writeVarLongArray(out, txPackets, bucketCount);
199 writeVarLongArray(out, operations, bucketCount);
203 public int describeContents() {
211 public long getBucketDuration() {
212 return bucketDuration;
215 public long getStart() {
216 if (bucketCount > 0) {
217 return bucketStart[0];
219 return Long.MAX_VALUE;
223 public long getEnd() {
224 if (bucketCount > 0) {
225 return bucketStart[bucketCount - 1] + bucketDuration;
227 return Long.MIN_VALUE;
232 * Return total bytes represented by this history.
234 public long getTotalBytes() {
239 * Return index of bucket that contains or is immediately before the
242 public int getIndexBefore(long time) {
243 int index = Arrays.binarySearch(bucketStart, 0, bucketCount, time);
245 index = (~index) - 1;
249 return MathUtils.constrain(index, 0, bucketCount - 1);
253 * Return index of bucket that contains or is immediately after the
256 public int getIndexAfter(long time) {
257 int index = Arrays.binarySearch(bucketStart, 0, bucketCount, time);
263 return MathUtils.constrain(index, 0, bucketCount - 1);
267 * Return specific stats entry.
269 public Entry getValues(int i, Entry recycle) {
270 final Entry entry = recycle != null ? recycle : new Entry();
271 entry.bucketStart = bucketStart[i];
272 entry.bucketDuration = bucketDuration;
273 entry.activeTime = getLong(activeTime, i, UNKNOWN);
274 entry.rxBytes = getLong(rxBytes, i, UNKNOWN);
275 entry.rxPackets = getLong(rxPackets, i, UNKNOWN);
276 entry.txBytes = getLong(txBytes, i, UNKNOWN);
277 entry.txPackets = getLong(txPackets, i, UNKNOWN);
278 entry.operations = getLong(operations, i, UNKNOWN);
283 * Record that data traffic occurred in the given time range. Will
284 * distribute across internal buckets, creating new buckets as needed.
287 public void recordData(long start, long end, long rxBytes, long txBytes) {
288 recordData(start, end, new NetworkStats.Entry(
289 IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, 0L, txBytes, 0L, 0L));
293 * Record that data traffic occurred in the given time range. Will
294 * distribute across internal buckets, creating new buckets as needed.
296 public void recordData(long start, long end, NetworkStats.Entry entry) {
297 long rxBytes = entry.rxBytes;
298 long rxPackets = entry.rxPackets;
299 long txBytes = entry.txBytes;
300 long txPackets = entry.txPackets;
301 long operations = entry.operations;
303 if (entry.isNegative()) {
304 throw new IllegalArgumentException("tried recording negative data");
306 if (entry.isEmpty()) {
310 // create any buckets needed by this range
311 ensureBuckets(start, end);
313 // distribute data usage into buckets
314 long duration = end - start;
315 final int startIndex = getIndexAfter(end);
316 for (int i = startIndex; i >= 0; i--) {
317 final long curStart = bucketStart[i];
318 final long curEnd = curStart + bucketDuration;
320 // bucket is older than record; we're finished
321 if (curEnd < start) break;
322 // bucket is newer than record; keep looking
323 if (curStart > end) continue;
325 final long overlap = Math.min(curEnd, end) - Math.max(curStart, start);
326 if (overlap <= 0) continue;
328 // integer math each time is faster than floating point
329 final long fracRxBytes = rxBytes * overlap / duration;
330 final long fracRxPackets = rxPackets * overlap / duration;
331 final long fracTxBytes = txBytes * overlap / duration;
332 final long fracTxPackets = txPackets * overlap / duration;
333 final long fracOperations = operations * overlap / duration;
335 addLong(activeTime, i, overlap);
336 addLong(this.rxBytes, i, fracRxBytes); rxBytes -= fracRxBytes;
337 addLong(this.rxPackets, i, fracRxPackets); rxPackets -= fracRxPackets;
338 addLong(this.txBytes, i, fracTxBytes); txBytes -= fracTxBytes;
339 addLong(this.txPackets, i, fracTxPackets); txPackets -= fracTxPackets;
340 addLong(this.operations, i, fracOperations); operations -= fracOperations;
345 totalBytes += entry.rxBytes + entry.txBytes;
349 * Record an entire {@link NetworkStatsHistory} into this history. Usually
350 * for combining together stats for external reporting.
352 public void recordEntireHistory(NetworkStatsHistory input) {
353 recordHistory(input, Long.MIN_VALUE, Long.MAX_VALUE);
357 * Record given {@link NetworkStatsHistory} into this history, copying only
358 * buckets that atomically occur in the inclusive time range. Doesn't
359 * interpolate across partial buckets.
361 public void recordHistory(NetworkStatsHistory input, long start, long end) {
362 final NetworkStats.Entry entry = new NetworkStats.Entry(
363 IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
364 for (int i = 0; i < input.bucketCount; i++) {
365 final long bucketStart = input.bucketStart[i];
366 final long bucketEnd = bucketStart + input.bucketDuration;
368 // skip when bucket is outside requested range
369 if (bucketStart < start || bucketEnd > end) continue;
371 entry.rxBytes = getLong(input.rxBytes, i, 0L);
372 entry.rxPackets = getLong(input.rxPackets, i, 0L);
373 entry.txBytes = getLong(input.txBytes, i, 0L);
374 entry.txPackets = getLong(input.txPackets, i, 0L);
375 entry.operations = getLong(input.operations, i, 0L);
377 recordData(bucketStart, bucketEnd, entry);
382 * Ensure that buckets exist for given time range, creating as needed.
384 private void ensureBuckets(long start, long end) {
385 // normalize incoming range to bucket boundaries
386 start -= start % bucketDuration;
387 end += (bucketDuration - (end % bucketDuration)) % bucketDuration;
389 for (long now = start; now < end; now += bucketDuration) {
390 // try finding existing bucket
391 final int index = Arrays.binarySearch(bucketStart, 0, bucketCount, now);
393 // bucket missing, create and insert
394 insertBucket(~index, now);
400 * Insert new bucket at requested index and starting time.
402 private void insertBucket(int index, long start) {
403 // create more buckets when needed
404 if (bucketCount >= bucketStart.length) {
405 final int newLength = Math.max(bucketStart.length, 10) * 3 / 2;
406 bucketStart = Arrays.copyOf(bucketStart, newLength);
407 if (activeTime != null) activeTime = Arrays.copyOf(activeTime, newLength);
408 if (rxBytes != null) rxBytes = Arrays.copyOf(rxBytes, newLength);
409 if (rxPackets != null) rxPackets = Arrays.copyOf(rxPackets, newLength);
410 if (txBytes != null) txBytes = Arrays.copyOf(txBytes, newLength);
411 if (txPackets != null) txPackets = Arrays.copyOf(txPackets, newLength);
412 if (operations != null) operations = Arrays.copyOf(operations, newLength);
415 // create gap when inserting bucket in middle
416 if (index < bucketCount) {
417 final int dstPos = index + 1;
418 final int length = bucketCount - index;
420 System.arraycopy(bucketStart, index, bucketStart, dstPos, length);
421 if (activeTime != null) System.arraycopy(activeTime, index, activeTime, dstPos, length);
422 if (rxBytes != null) System.arraycopy(rxBytes, index, rxBytes, dstPos, length);
423 if (rxPackets != null) System.arraycopy(rxPackets, index, rxPackets, dstPos, length);
424 if (txBytes != null) System.arraycopy(txBytes, index, txBytes, dstPos, length);
425 if (txPackets != null) System.arraycopy(txPackets, index, txPackets, dstPos, length);
426 if (operations != null) System.arraycopy(operations, index, operations, dstPos, length);
429 bucketStart[index] = start;
430 setLong(activeTime, index, 0L);
431 setLong(rxBytes, index, 0L);
432 setLong(rxPackets, index, 0L);
433 setLong(txBytes, index, 0L);
434 setLong(txPackets, index, 0L);
435 setLong(operations, index, 0L);
440 * Remove buckets older than requested cutoff.
443 public void removeBucketsBefore(long cutoff) {
445 for (i = 0; i < bucketCount; i++) {
446 final long curStart = bucketStart[i];
447 final long curEnd = curStart + bucketDuration;
449 // cutoff happens before or during this bucket; everything before
450 // this bucket should be removed.
451 if (curEnd > cutoff) break;
455 final int length = bucketStart.length;
456 bucketStart = Arrays.copyOfRange(bucketStart, i, length);
457 if (activeTime != null) activeTime = Arrays.copyOfRange(activeTime, i, length);
458 if (rxBytes != null) rxBytes = Arrays.copyOfRange(rxBytes, i, length);
459 if (rxPackets != null) rxPackets = Arrays.copyOfRange(rxPackets, i, length);
460 if (txBytes != null) txBytes = Arrays.copyOfRange(txBytes, i, length);
461 if (txPackets != null) txPackets = Arrays.copyOfRange(txPackets, i, length);
462 if (operations != null) operations = Arrays.copyOfRange(operations, i, length);
465 // TODO: subtract removed values from totalBytes
470 * Return interpolated data usage across the requested range. Interpolates
471 * across buckets, so values may be rounded slightly.
473 public Entry getValues(long start, long end, Entry recycle) {
474 return getValues(start, end, Long.MAX_VALUE, recycle);
478 * Return interpolated data usage across the requested range. Interpolates
479 * across buckets, so values may be rounded slightly.
481 public Entry getValues(long start, long end, long now, Entry recycle) {
482 final Entry entry = recycle != null ? recycle : new Entry();
483 entry.bucketDuration = end - start;
484 entry.bucketStart = start;
485 entry.activeTime = activeTime != null ? 0 : UNKNOWN;
486 entry.rxBytes = rxBytes != null ? 0 : UNKNOWN;
487 entry.rxPackets = rxPackets != null ? 0 : UNKNOWN;
488 entry.txBytes = txBytes != null ? 0 : UNKNOWN;
489 entry.txPackets = txPackets != null ? 0 : UNKNOWN;
490 entry.operations = operations != null ? 0 : UNKNOWN;
492 final int startIndex = getIndexAfter(end);
493 for (int i = startIndex; i >= 0; i--) {
494 final long curStart = bucketStart[i];
495 final long curEnd = curStart + bucketDuration;
497 // bucket is older than request; we're finished
498 if (curEnd <= start) break;
499 // bucket is newer than request; keep looking
500 if (curStart >= end) continue;
502 // include full value for active buckets, otherwise only fractional
503 final boolean activeBucket = curStart < now && curEnd > now;
506 overlap = bucketDuration;
508 final long overlapEnd = curEnd < end ? curEnd : end;
509 final long overlapStart = curStart > start ? curStart : start;
510 overlap = overlapEnd - overlapStart;
512 if (overlap <= 0) continue;
514 // integer math each time is faster than floating point
515 if (activeTime != null) entry.activeTime += activeTime[i] * overlap / bucketDuration;
516 if (rxBytes != null) entry.rxBytes += rxBytes[i] * overlap / bucketDuration;
517 if (rxPackets != null) entry.rxPackets += rxPackets[i] * overlap / bucketDuration;
518 if (txBytes != null) entry.txBytes += txBytes[i] * overlap / bucketDuration;
519 if (txPackets != null) entry.txPackets += txPackets[i] * overlap / bucketDuration;
520 if (operations != null) entry.operations += operations[i] * overlap / bucketDuration;
526 * @deprecated only for temporary testing
529 public void generateRandom(long start, long end, long bytes) {
530 final Random r = new Random();
532 final float fractionRx = r.nextFloat();
533 final long rxBytes = (long) (bytes * fractionRx);
534 final long txBytes = (long) (bytes * (1 - fractionRx));
536 final long rxPackets = rxBytes / 1024;
537 final long txPackets = txBytes / 1024;
538 final long operations = rxBytes / 2048;
540 generateRandom(start, end, rxBytes, rxPackets, txBytes, txPackets, operations, r);
544 * @deprecated only for temporary testing
547 public void generateRandom(long start, long end, long rxBytes, long rxPackets, long txBytes,
548 long txPackets, long operations, Random r) {
549 ensureBuckets(start, end);
551 final NetworkStats.Entry entry = new NetworkStats.Entry(
552 IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
553 while (rxBytes > 1024 || rxPackets > 128 || txBytes > 1024 || txPackets > 128
554 || operations > 32) {
555 final long curStart = randomLong(r, start, end);
556 final long curEnd = curStart + randomLong(r, 0, (end - curStart) / 2);
558 entry.rxBytes = randomLong(r, 0, rxBytes);
559 entry.rxPackets = randomLong(r, 0, rxPackets);
560 entry.txBytes = randomLong(r, 0, txBytes);
561 entry.txPackets = randomLong(r, 0, txPackets);
562 entry.operations = randomLong(r, 0, operations);
564 rxBytes -= entry.rxBytes;
565 rxPackets -= entry.rxPackets;
566 txBytes -= entry.txBytes;
567 txPackets -= entry.txPackets;
568 operations -= entry.operations;
570 recordData(curStart, curEnd, entry);
574 public static long randomLong(Random r, long start, long end) {
575 return (long) (start + (r.nextFloat() * (end - start)));
579 * Quickly determine if this history intersects with given window.
581 public boolean intersects(long start, long end) {
582 final long dataStart = getStart();
583 final long dataEnd = getEnd();
584 if (start >= dataStart && start <= dataEnd) return true;
585 if (end >= dataStart && end <= dataEnd) return true;
586 if (dataStart >= start && dataStart <= end) return true;
587 if (dataEnd >= start && dataEnd <= end) return true;
591 public void dump(IndentingPrintWriter pw, boolean fullHistory) {
592 pw.print("NetworkStatsHistory: bucketDuration=");
593 pw.println(bucketDuration / SECOND_IN_MILLIS);
596 final int start = fullHistory ? 0 : Math.max(0, bucketCount - 32);
598 pw.print("(omitting "); pw.print(start); pw.println(" buckets)");
601 for (int i = start; i < bucketCount; i++) {
602 pw.print("st="); pw.print(bucketStart[i] / SECOND_IN_MILLIS);
603 if (rxBytes != null) { pw.print(" rb="); pw.print(rxBytes[i]); }
604 if (rxPackets != null) { pw.print(" rp="); pw.print(rxPackets[i]); }
605 if (txBytes != null) { pw.print(" tb="); pw.print(txBytes[i]); }
606 if (txPackets != null) { pw.print(" tp="); pw.print(txPackets[i]); }
607 if (operations != null) { pw.print(" op="); pw.print(operations[i]); }
614 public void dumpCheckin(PrintWriter pw) {
616 pw.print(bucketDuration / SECOND_IN_MILLIS);
619 for (int i = 0; i < bucketCount; i++) {
621 pw.print(bucketStart[i] / SECOND_IN_MILLIS); pw.print(',');
622 if (rxBytes != null) { pw.print(rxBytes[i]); } else { pw.print("*"); } pw.print(',');
623 if (rxPackets != null) { pw.print(rxPackets[i]); } else { pw.print("*"); } pw.print(',');
624 if (txBytes != null) { pw.print(txBytes[i]); } else { pw.print("*"); } pw.print(',');
625 if (txPackets != null) { pw.print(txPackets[i]); } else { pw.print("*"); } pw.print(',');
626 if (operations != null) { pw.print(operations[i]); } else { pw.print("*"); }
632 public String toString() {
633 final CharArrayWriter writer = new CharArrayWriter();
634 dump(new IndentingPrintWriter(writer, " "), false);
635 return writer.toString();
638 public static final Creator<NetworkStatsHistory> CREATOR = new Creator<NetworkStatsHistory>() {
640 public NetworkStatsHistory createFromParcel(Parcel in) {
641 return new NetworkStatsHistory(in);
645 public NetworkStatsHistory[] newArray(int size) {
646 return new NetworkStatsHistory[size];
650 private static long getLong(long[] array, int i, long value) {
651 return array != null ? array[i] : value;
654 private static void setLong(long[] array, int i, long value) {
655 if (array != null) array[i] = value;
658 private static void addLong(long[] array, int i, long value) {
659 if (array != null) array[i] += value;
662 public int estimateResizeBuckets(long newBucketDuration) {
663 return (int) (size() * getBucketDuration() / newBucketDuration);
667 * Utility methods for interacting with {@link DataInputStream} and
668 * {@link DataOutputStream}, mostly dealing with writing partial arrays.
670 public static class DataStreamUtils {
672 public static long[] readFullLongArray(DataInputStream in) throws IOException {
673 final int size = in.readInt();
674 if (size < 0) throw new ProtocolException("negative array size");
675 final long[] values = new long[size];
676 for (int i = 0; i < values.length; i++) {
677 values[i] = in.readLong();
683 * Read variable-length {@link Long} using protobuf-style approach.
685 public static long readVarLong(DataInputStream in) throws IOException {
689 byte b = in.readByte();
690 result |= (long) (b & 0x7F) << shift;
695 throw new ProtocolException("malformed long");
699 * Write variable-length {@link Long} using protobuf-style approach.
701 public static void writeVarLong(DataOutputStream out, long value) throws IOException {
703 if ((value & ~0x7FL) == 0) {
704 out.writeByte((int) value);
707 out.writeByte(((int) value & 0x7F) | 0x80);
713 public static long[] readVarLongArray(DataInputStream in) throws IOException {
714 final int size = in.readInt();
715 if (size == -1) return null;
716 if (size < 0) throw new ProtocolException("negative array size");
717 final long[] values = new long[size];
718 for (int i = 0; i < values.length; i++) {
719 values[i] = readVarLong(in);
724 public static void writeVarLongArray(DataOutputStream out, long[] values, int size)
726 if (values == null) {
730 if (size > values.length) {
731 throw new IllegalArgumentException("size larger than length");
734 for (int i = 0; i < size; i++) {
735 writeVarLong(out, values[i]);
741 * Utility methods for interacting with {@link Parcel} structures, mostly
742 * dealing with writing partial arrays.
744 public static class ParcelUtils {
745 public static long[] readLongArray(Parcel in) {
746 final int size = in.readInt();
747 if (size == -1) return null;
748 final long[] values = new long[size];
749 for (int i = 0; i < values.length; i++) {
750 values[i] = in.readLong();
755 public static void writeLongArray(Parcel out, long[] values, int size) {
756 if (values == null) {
760 if (size > values.length) {
761 throw new IllegalArgumentException("size larger than length");
764 for (int i = 0; i < size; i++) {
765 out.writeLong(values[i]);