atomic-storage: remove type dependency at segment level I/O
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ChunkedByteArray.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
10 import static java.util.Objects.requireNonNull;
11
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;
21
22 @NonNullByDefault
23 final class ChunkedByteArray implements Immutable {
24     private final ImmutableList<byte[]> chunks;
25     private final int size;
26
27     ChunkedByteArray(final int size, final ImmutableList<byte[]> chunks) {
28         this.size = size;
29         this.chunks = requireNonNull(chunks);
30     }
31
32     static ChunkedByteArray readFrom(final ObjectInput in, final int size, final int chunkSize)
33             throws IOException {
34         final List<byte[]> chunks = new ArrayList<>(requiredChunks(size, chunkSize));
35         int remaining = size;
36         do {
37             final byte[] buffer = new byte[Math.min(remaining, chunkSize)];
38             in.readFully(buffer);
39             chunks.add(buffer);
40             remaining -= buffer.length;
41         } while (remaining != 0);
42
43         return new ChunkedByteArray(size, ImmutableList.copyOf(chunks));
44     }
45
46     int size() {
47         return size;
48     }
49
50     ChunkedInputStream openStream() {
51         return new ChunkedInputStream(size, chunks.iterator());
52     }
53
54     void copyTo(final DataOutput output) throws IOException {
55         for (byte[] chunk : chunks) {
56             output.write(chunk, 0, chunk.length);
57         }
58     }
59
60     @Override
61     public String toString() {
62         return MoreObjects.toStringHelper(this).add("size", size).add("chunkCount", chunks.size()).toString();
63     }
64
65     ImmutableList<byte[]> getChunks() {
66         return chunks;
67     }
68
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;
72     }
73 }