2 * Copyright (c) 2020 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.mdsal.replicate.netty;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.util.List;
20 * An OutputStream which makes sure to slice messages to a maximum size. This prevents array reallocations and
21 * GC thrashing on huge objects.
23 final class SplittingOutputStream extends OutputStream {
24 private static final int INIT_BUF = 4096;
27 verify(INIT_BUF <= Constants.LENGTH_FIELD_MAX);
30 private final List<Object> out;
34 SplittingOutputStream(final List<Object> out) {
35 this.out = requireNonNull(out);
40 @SuppressWarnings("checkstyle:parameterName")
41 public void write(final int b) throws IOException {
47 @SuppressWarnings("checkstyle:parameterName")
48 public void write(final byte[] b, final int off, final int len) throws IOException {
49 if (off < 0 || len < 0) {
50 throw new IndexOutOfBoundsException();
56 final int chunk = Math.min(Constants.LENGTH_FIELD_MAX - buf.writerIndex(), left);
58 buf.writeBytes(b, ptr, chunk);
67 if (buf.writerIndex() != 0) {
73 private void allocBuffer() {
74 // FIXME: use buffer allocator?
75 buf = Unpooled.buffer(INIT_BUF, Constants.LENGTH_FIELD_MAX);
76 buf.writeByte(Constants.MSG_DTC_CHUNK);
79 private void checkSend() {
80 if (buf.writerIndex() == Constants.LENGTH_FIELD_MAX) {