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;
18 import java.util.Objects;
21 * An OutputStream which makes sure to slice messages to a maximum size. This prevents array reallocations and
22 * GC thrashing on huge objects.
24 final class SplittingOutputStream extends OutputStream {
25 private static final int INIT_BUF = 4096;
28 verify(INIT_BUF <= Constants.LENGTH_FIELD_MAX);
31 private final List<Object> out;
35 SplittingOutputStream(final List<Object> out) {
36 this.out = requireNonNull(out);
41 @SuppressWarnings("checkstyle:parameterName")
42 public void write(final int b) throws IOException {
48 @SuppressWarnings("checkstyle:parameterName")
49 public void write(final byte[] b, final int off, final int len) throws IOException {
50 if (off < 0 || len < 0) {
51 throw new IndexOutOfBoundsException();
57 final int chunk = Math.min(Constants.LENGTH_FIELD_MAX - buf.writerIndex(), left);
59 buf.writeBytes(b, ptr, chunk);
68 if (buf.writerIndex() != 0) {
74 private void allocBuffer() {
75 // FIXME: use buffer allocator?
76 buf = Unpooled.buffer(INIT_BUF, Constants.LENGTH_FIELD_MAX);
77 buf.writeByte(Constants.MSG_DTC_CHUNK);
80 private void checkSend() {
81 if (buf.writerIndex() == Constants.LENGTH_FIELD_MAX) {