2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.persisted;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.ImmutableList;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.util.ArrayList;
18 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.yangtools.concepts.Immutable;
23 final class ChunkedByteArray implements Immutable {
24 private final ImmutableList<byte[]> chunks;
25 private final int size;
27 ChunkedByteArray(final int size, final ImmutableList<byte[]> chunks) {
29 this.chunks = requireNonNull(chunks);
32 static ChunkedByteArray readFrom(final ObjectInput in, final int size, final int chunkSize)
34 final List<byte[]> chunks = new ArrayList<>(requiredChunks(size, chunkSize));
37 final byte[] buffer = new byte[Math.min(remaining, chunkSize)];
40 remaining -= buffer.length;
41 } while (remaining != 0);
43 return new ChunkedByteArray(size, ImmutableList.copyOf(chunks));
50 ChunkedInputStream openStream() {
51 return new ChunkedInputStream(size, chunks.iterator());
54 void copyTo(final DataOutput output) throws IOException {
55 for (byte[] chunk : chunks) {
56 output.write(chunk, 0, chunk.length);
61 public String toString() {
62 return MoreObjects.toStringHelper(this).add("size", size).add("chunkCount", chunks.size()).toString();
65 ImmutableList<byte[]> getChunks() {
69 private static int requiredChunks(final int size, final int chunkSize) {
70 final int div = size / chunkSize;
71 return size % chunkSize == 0 ? div : div + 1;