dan-s1 commented on code in PR #8691:
URL: https://github.com/apache/nifi/pull/8691#discussion_r1643390405


##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors-nar/src/main/resources/META-INF/NOTICE:
##########
@@ -4,6 +4,14 @@ Copyright 2017-2020 The Apache Software Foundation
 This product includes software developed at
 The Apache Software Foundation (http://www.apache.org/).
 
+This project includes derived works from the Kaitai Project available under 
the MIT License. 
+  Portions of code found at https://formats.kaitai.io/pcap/java.html
+  Copyright 2015-2024 Kaitai Project
+  The code can be found in:
+    
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/ByteBufferReader.java
+    
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/Header.java

Review Comment:
   This needs to be updated
   ```suggestion
       
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/PCAPHeader.java
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/SplitPCAP.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.network.pcap;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.flowfile.attributes.FragmentAttributes;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.io.InputStreamCallback;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+@SideEffectFree
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@Tags({"PCAP", "Splitter", "Network", "Packet", "Capture", "Wireshark", 
"TShark", "TcpDump", "WinDump", "sniffers"})
+@CapabilityDescription("Splits one pcap file into multiple pcap files based on 
a maximum size.")
+@WritesAttributes({
+    @WritesAttribute(
+        attribute = SplitPCAP.ERROR_REASON_LABEL,
+        description = "The reason the flowfile was sent to the failure 
relationship."
+    ),
+    @WritesAttribute(
+        attribute = "fragment.identifier",
+        description = "All split PCAP FlowFiles produced from the same parent 
PCAP FlowFile will have the same randomly generated UUID added for this 
attribute"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.index",
+        description = "A one-up number that indicates the ordering of the 
split PCAP FlowFiles that were created from a single parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.count",
+        description = "The number of split PCAP FlowFiles generated from the 
parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "segment.original.filename ",
+        description = "The filename of the parent PCAP FlowFile"
+    )
+})
+
+public class SplitPCAP extends AbstractProcessor {
+
+    protected static final String ERROR_REASON_LABEL = "ERROR_REASON";
+    public static final String FRAGMENT_ID = 
FragmentAttributes.FRAGMENT_ID.key();
+    public static final String FRAGMENT_INDEX = 
FragmentAttributes.FRAGMENT_INDEX.key();
+    public static final String FRAGMENT_COUNT = 
FragmentAttributes.FRAGMENT_COUNT.key();
+    public static final String SEGMENT_ORIGINAL_FILENAME = 
FragmentAttributes.SEGMENT_ORIGINAL_FILENAME.key();
+
+    public static final PropertyDescriptor PCAP_MAX_SIZE = new 
PropertyDescriptor
+            .Builder().name("PCAP Max Size")
+            .displayName("PCAP Max Size")
+            .description("Maximum size of each output PCAP file. PCAP packets 
larger than the configured size result in routing FlowFiles to the failure 
relationship.")
+            .required(true)
+            .defaultValue("1 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_ORIGINAL = new Relationship.Builder()
+            .name("original")
+            .description("The original FlowFile that was split into segments. 
If the FlowFile fails processing, nothing will be sent to "
+            + "this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile cannot be transformed from the 
configured input format to the configured output format, "
+            + "the unchanged FlowFile will be routed to this relationship.")
+            .build();
+    public static final Relationship REL_SPLIT = new Relationship.Builder()
+            .name("split")
+            .description("The individual PCAP 'segments' of the original PCAP 
FlowFile will be routed to this relationship.")
+            .build();
+
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
List.of(PCAP_MAX_SIZE);
+    private static final Set<Relationship> RELATIONSHIPS = 
Set.of(REL_ORIGINAL, REL_FAILURE, REL_SPLIT);
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return RELATIONSHIPS;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    /**
+     * This method is called when a trigger event occurs in the processor.
+     * It processes the incoming flow file, splits it into smaller pcap files 
based on the PCAP Max Size,
+     * and transfers the split pcap files to the success relationship.
+     * If the flow file is empty or not parseable, it is transferred to the 
failure relationship.
+     *
+     * @param context  the process context
+     * @param session  the process session
+     */
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) {
+
+        final int pcapMaxSize = 
context.getProperty(PCAP_MAX_SIZE.getName()).asDataSize(DataUnit.B).intValue();
+        FlowFile originalFlowFile = session.get();
+        if (originalFlowFile == null) {
+            return;
+        }
+
+        final List<FlowFile> splitFiles = new ArrayList<>();
+
+        PCAPStreamSplitterCallback callback = new 
PCAPStreamSplitterCallback(session, originalFlowFile, pcapMaxSize);
+
+        try {
+            session.read(originalFlowFile, callback);
+        } catch (ProcessException e) {
+            splitFiles.addAll(callback.getSplitFiles());
+
+            getLogger().error("Failed to split {}", originalFlowFile, e);
+            session.remove(splitFiles);
+            splitFiles.clear();
+            session.putAttribute(originalFlowFile, ERROR_REASON_LABEL, 
e.getMessage());
+            session.transfer(originalFlowFile, REL_FAILURE);
+            return;
+        }
+
+        splitFiles.addAll(callback.getSplitFiles());
+
+        final String fragmentId = UUID.randomUUID().toString();
+        final String originalFileName = 
originalFlowFile.getAttribute(CoreAttributes.FILENAME.key());
+        final String originalFileNameWithoutExtension = 
originalFileName.substring(0, originalFileName.lastIndexOf("."));
+
+        final Map<String, String> attributes = new HashMap<>();
+        attributes.put(FRAGMENT_COUNT, String.valueOf(splitFiles.size()));
+        attributes.put(FRAGMENT_ID, fragmentId);
+        attributes.put(SEGMENT_ORIGINAL_FILENAME, originalFileName);
+
+        IntStream.range(0, splitFiles.size()).forEach(index -> {
+            FlowFile split = splitFiles.get(index);
+            attributes.put(CoreAttributes.FILENAME.key(), 
"%s-%d.pcap".formatted(originalFileNameWithoutExtension, index));
+            attributes.put(FRAGMENT_INDEX, Integer.toString(index));
+            session.transfer(session.putAllAttributes(split, attributes), 
REL_SPLIT);
+        });
+        session.transfer(originalFlowFile, REL_ORIGINAL);
+    }
+
+    protected class PCAPStreamSplitterCallback implements InputStreamCallback {
+        private final ProcessSession session;
+        private final FlowFile originalFlowFile;
+        private final int pcapMaxSize;
+        private final List<FlowFile> splitFiles = new ArrayList<>();
+
+        public List<FlowFile> getSplitFiles() {
+            return splitFiles;
+        }
+
+        public PCAPStreamSplitterCallback( ProcessSession session, FlowFile 
flowFile, int pcapMaxSize) {
+            this.session = session;
+            this.originalFlowFile = flowFile;
+            this.pcapMaxSize = pcapMaxSize;
+        }
+
+        private Packet getNextPacket(BufferedInputStream bufferedStream, PCAP 
templatePcap, int totalPackets) throws IOException {
+            byte[] packetHeader = new byte[Packet.PACKET_HEADER_LENGTH];
+            bufferedStream.read(packetHeader, 0, Packet.PACKET_HEADER_LENGTH);
+            Packet currentPacket = new Packet(packetHeader, templatePcap);
+
+            if (currentPacket.totalLength() > this.pcapMaxSize) {
+                throw new ProcessException("PCAP Packet length [%d] larger 
then configured maximum [%d]".formatted(currentPacket.totalLength(), 
pcapMaxSize));
+            }
+
+            byte[] packetbody = new byte[(int) currentPacket.expectedLength()];
+
+            bufferedStream.read(packetbody, 0, (int) 
currentPacket.expectedLength());
+            currentPacket.setBody(packetbody);
+
+            if (currentPacket.isInvalid()) {
+                throw new ProcessException("PCAP contains an invalid packet. 
Packet number [%d] is invalid - [%s]".formatted(totalPackets, 
currentPacket.invalidityReason()));
+            }
+
+            return currentPacket;
+        }
+
+        @Override
+        public void process(InputStream inStream) throws IOException {
+            final List<Packet> loadedPackets = new ArrayList<>();
+            final BufferedInputStream bufferedStream = new 
BufferedInputStream(inStream);
+            int totalPackets = 1;
+
+            if ( bufferedStream.available() == 0 ) {

Review Comment:
   ```suggestion
               if (bufferedStream.available() == 0) {
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/SplitPCAP.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.network.pcap;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.flowfile.attributes.FragmentAttributes;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.io.InputStreamCallback;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+@SideEffectFree
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@Tags({"PCAP", "Splitter", "Network", "Packet", "Capture", "Wireshark", 
"TShark", "TcpDump", "WinDump", "sniffers"})
+@CapabilityDescription("Splits one pcap file into multiple pcap files based on 
a maximum size.")
+@WritesAttributes({
+    @WritesAttribute(
+        attribute = SplitPCAP.ERROR_REASON_LABEL,
+        description = "The reason the flowfile was sent to the failure 
relationship."
+    ),
+    @WritesAttribute(
+        attribute = "fragment.identifier",
+        description = "All split PCAP FlowFiles produced from the same parent 
PCAP FlowFile will have the same randomly generated UUID added for this 
attribute"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.index",
+        description = "A one-up number that indicates the ordering of the 
split PCAP FlowFiles that were created from a single parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.count",
+        description = "The number of split PCAP FlowFiles generated from the 
parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "segment.original.filename ",
+        description = "The filename of the parent PCAP FlowFile"
+    )
+})
+
+public class SplitPCAP extends AbstractProcessor {
+
+    protected static final String ERROR_REASON_LABEL = "ERROR_REASON";
+    public static final String FRAGMENT_ID = 
FragmentAttributes.FRAGMENT_ID.key();
+    public static final String FRAGMENT_INDEX = 
FragmentAttributes.FRAGMENT_INDEX.key();
+    public static final String FRAGMENT_COUNT = 
FragmentAttributes.FRAGMENT_COUNT.key();
+    public static final String SEGMENT_ORIGINAL_FILENAME = 
FragmentAttributes.SEGMENT_ORIGINAL_FILENAME.key();
+
+    public static final PropertyDescriptor PCAP_MAX_SIZE = new 
PropertyDescriptor
+            .Builder().name("PCAP Max Size")
+            .displayName("PCAP Max Size")
+            .description("Maximum size of each output PCAP file. PCAP packets 
larger than the configured size result in routing FlowFiles to the failure 
relationship.")
+            .required(true)
+            .defaultValue("1 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_ORIGINAL = new Relationship.Builder()
+            .name("original")
+            .description("The original FlowFile that was split into segments. 
If the FlowFile fails processing, nothing will be sent to "
+            + "this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile cannot be transformed from the 
configured input format to the configured output format, "
+            + "the unchanged FlowFile will be routed to this relationship.")
+            .build();
+    public static final Relationship REL_SPLIT = new Relationship.Builder()
+            .name("split")
+            .description("The individual PCAP 'segments' of the original PCAP 
FlowFile will be routed to this relationship.")
+            .build();
+
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
List.of(PCAP_MAX_SIZE);
+    private static final Set<Relationship> RELATIONSHIPS = 
Set.of(REL_ORIGINAL, REL_FAILURE, REL_SPLIT);
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return RELATIONSHIPS;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    /**
+     * This method is called when a trigger event occurs in the processor.
+     * It processes the incoming flow file, splits it into smaller pcap files 
based on the PCAP Max Size,
+     * and transfers the split pcap files to the success relationship.
+     * If the flow file is empty or not parseable, it is transferred to the 
failure relationship.
+     *
+     * @param context  the process context
+     * @param session  the process session
+     */
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) {
+
+        final int pcapMaxSize = 
context.getProperty(PCAP_MAX_SIZE.getName()).asDataSize(DataUnit.B).intValue();
+        FlowFile originalFlowFile = session.get();
+        if (originalFlowFile == null) {
+            return;
+        }
+
+        final List<FlowFile> splitFiles = new ArrayList<>();
+
+        PCAPStreamSplitterCallback callback = new 
PCAPStreamSplitterCallback(session, originalFlowFile, pcapMaxSize);
+
+        try {
+            session.read(originalFlowFile, callback);
+        } catch (ProcessException e) {
+            splitFiles.addAll(callback.getSplitFiles());
+
+            getLogger().error("Failed to split {}", originalFlowFile, e);
+            session.remove(splitFiles);
+            splitFiles.clear();

Review Comment:
   Per earlier comment
   ```suggestion
               getLogger().error("Failed to split {}", originalFlowFile, e);
               session.remove(callback.getSplitFiles());
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/ByteBufferReader.java:
##########
@@ -0,0 +1,69 @@
+// MIT License
+
+// Copyright (c) 2015-2024 Kaitai Project
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to 
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in 
all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
+// SOFTWARE.
+
+package org.apache.nifi.processors.network.pcap;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+public class ByteBufferReader {
+   final private ByteBuffer buffer;
+
+    public ByteBufferReader(byte[] byteArray) {
+        this.buffer = ByteBuffer.wrap(byteArray);
+    }
+
+    public int readU2() {
+        buffer.order(ByteOrder.LITTLE_ENDIAN);
+        return (buffer.getShort() & 0xffff);
+    }
+
+    public long readU4() {
+        buffer.order(ByteOrder.LITTLE_ENDIAN);
+        return ((long) buffer.getInt() & 0xffffffffL);
+    }
+
+    public int readS4() {
+        buffer.order(ByteOrder.LITTLE_ENDIAN);
+        return buffer.getInt();
+    }

Review Comment:
   Since `LITTLE_ENDIAN` will be the default byte order, shouldn't you just set 
it once in the constructor?
   
   ```suggestion
       public ByteBufferReader(byte[] byteArray) {
           this.buffer = ByteBuffer.wrap(byteArray);
           this.buffer.order(ByteOrder.LITTLE_ENDIAN);
       }
   
       public int readU2() {
           return (buffer.getShort() & 0xffff);
       }
   
       public long readU4() {
           return ((long) buffer.getInt() & 0xffffffffL);
       }
   
       public int readS4() {
           return buffer.getInt();
       }
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/SplitPCAP.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.network.pcap;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.flowfile.attributes.FragmentAttributes;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.io.InputStreamCallback;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+@SideEffectFree
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@Tags({"PCAP", "Splitter", "Network", "Packet", "Capture", "Wireshark", 
"TShark", "TcpDump", "WinDump", "sniffers"})
+@CapabilityDescription("Splits one pcap file into multiple pcap files based on 
a maximum size.")
+@WritesAttributes({
+    @WritesAttribute(
+        attribute = SplitPCAP.ERROR_REASON_LABEL,
+        description = "The reason the flowfile was sent to the failure 
relationship."
+    ),
+    @WritesAttribute(
+        attribute = "fragment.identifier",
+        description = "All split PCAP FlowFiles produced from the same parent 
PCAP FlowFile will have the same randomly generated UUID added for this 
attribute"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.index",
+        description = "A one-up number that indicates the ordering of the 
split PCAP FlowFiles that were created from a single parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.count",
+        description = "The number of split PCAP FlowFiles generated from the 
parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "segment.original.filename ",
+        description = "The filename of the parent PCAP FlowFile"
+    )
+})
+
+public class SplitPCAP extends AbstractProcessor {
+
+    protected static final String ERROR_REASON_LABEL = "ERROR_REASON";
+    public static final String FRAGMENT_ID = 
FragmentAttributes.FRAGMENT_ID.key();
+    public static final String FRAGMENT_INDEX = 
FragmentAttributes.FRAGMENT_INDEX.key();
+    public static final String FRAGMENT_COUNT = 
FragmentAttributes.FRAGMENT_COUNT.key();
+    public static final String SEGMENT_ORIGINAL_FILENAME = 
FragmentAttributes.SEGMENT_ORIGINAL_FILENAME.key();
+
+    public static final PropertyDescriptor PCAP_MAX_SIZE = new 
PropertyDescriptor
+            .Builder().name("PCAP Max Size")
+            .displayName("PCAP Max Size")
+            .description("Maximum size of each output PCAP file. PCAP packets 
larger than the configured size result in routing FlowFiles to the failure 
relationship.")
+            .required(true)
+            .defaultValue("1 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_ORIGINAL = new Relationship.Builder()
+            .name("original")
+            .description("The original FlowFile that was split into segments. 
If the FlowFile fails processing, nothing will be sent to "
+            + "this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile cannot be transformed from the 
configured input format to the configured output format, "
+            + "the unchanged FlowFile will be routed to this relationship.")
+            .build();
+    public static final Relationship REL_SPLIT = new Relationship.Builder()
+            .name("split")
+            .description("The individual PCAP 'segments' of the original PCAP 
FlowFile will be routed to this relationship.")
+            .build();
+
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
List.of(PCAP_MAX_SIZE);
+    private static final Set<Relationship> RELATIONSHIPS = 
Set.of(REL_ORIGINAL, REL_FAILURE, REL_SPLIT);
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return RELATIONSHIPS;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    /**
+     * This method is called when a trigger event occurs in the processor.
+     * It processes the incoming flow file, splits it into smaller pcap files 
based on the PCAP Max Size,
+     * and transfers the split pcap files to the success relationship.
+     * If the flow file is empty or not parseable, it is transferred to the 
failure relationship.
+     *
+     * @param context  the process context
+     * @param session  the process session
+     */
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) {
+
+        final int pcapMaxSize = 
context.getProperty(PCAP_MAX_SIZE.getName()).asDataSize(DataUnit.B).intValue();
+        FlowFile originalFlowFile = session.get();
+        if (originalFlowFile == null) {
+            return;
+        }
+
+        final List<FlowFile> splitFiles = new ArrayList<>();
+
+        PCAPStreamSplitterCallback callback = new 
PCAPStreamSplitterCallback(session, originalFlowFile, pcapMaxSize);
+
+        try {
+            session.read(originalFlowFile, callback);
+        } catch (ProcessException e) {
+            splitFiles.addAll(callback.getSplitFiles());
+
+            getLogger().error("Failed to split {}", originalFlowFile, e);
+            session.remove(splitFiles);
+            splitFiles.clear();
+            session.putAttribute(originalFlowFile, ERROR_REASON_LABEL, 
e.getMessage());
+            session.transfer(originalFlowFile, REL_FAILURE);
+            return;
+        }
+
+        splitFiles.addAll(callback.getSplitFiles());
+
+        final String fragmentId = UUID.randomUUID().toString();
+        final String originalFileName = 
originalFlowFile.getAttribute(CoreAttributes.FILENAME.key());
+        final String originalFileNameWithoutExtension = 
originalFileName.substring(0, originalFileName.lastIndexOf("."));
+
+        final Map<String, String> attributes = new HashMap<>();
+        attributes.put(FRAGMENT_COUNT, String.valueOf(splitFiles.size()));
+        attributes.put(FRAGMENT_ID, fragmentId);
+        attributes.put(SEGMENT_ORIGINAL_FILENAME, originalFileName);
+
+        IntStream.range(0, splitFiles.size()).forEach(index -> {
+            FlowFile split = splitFiles.get(index);
+            attributes.put(CoreAttributes.FILENAME.key(), 
"%s-%d.pcap".formatted(originalFileNameWithoutExtension, index));
+            attributes.put(FRAGMENT_INDEX, Integer.toString(index));
+            session.transfer(session.putAllAttributes(split, attributes), 
REL_SPLIT);
+        });
+        session.transfer(originalFlowFile, REL_ORIGINAL);
+    }
+
+    protected class PCAPStreamSplitterCallback implements InputStreamCallback {
+        private final ProcessSession session;
+        private final FlowFile originalFlowFile;
+        private final int pcapMaxSize;
+        private final List<FlowFile> splitFiles = new ArrayList<>();
+
+        public List<FlowFile> getSplitFiles() {
+            return splitFiles;
+        }
+
+        public PCAPStreamSplitterCallback( ProcessSession session, FlowFile 
flowFile, int pcapMaxSize) {

Review Comment:
   ```suggestion
           public PCAPStreamSplitterCallback(ProcessSession session, FlowFile 
flowFile, int pcapMaxSize) {
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/SplitPCAP.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.network.pcap;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.flowfile.attributes.FragmentAttributes;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.io.InputStreamCallback;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+@SideEffectFree
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@Tags({"PCAP", "Splitter", "Network", "Packet", "Capture", "Wireshark", 
"TShark", "TcpDump", "WinDump", "sniffers"})
+@CapabilityDescription("Splits one pcap file into multiple pcap files based on 
a maximum size.")
+@WritesAttributes({
+    @WritesAttribute(
+        attribute = SplitPCAP.ERROR_REASON_LABEL,
+        description = "The reason the flowfile was sent to the failure 
relationship."
+    ),
+    @WritesAttribute(
+        attribute = "fragment.identifier",
+        description = "All split PCAP FlowFiles produced from the same parent 
PCAP FlowFile will have the same randomly generated UUID added for this 
attribute"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.index",
+        description = "A one-up number that indicates the ordering of the 
split PCAP FlowFiles that were created from a single parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.count",
+        description = "The number of split PCAP FlowFiles generated from the 
parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "segment.original.filename ",
+        description = "The filename of the parent PCAP FlowFile"
+    )
+})
+
+public class SplitPCAP extends AbstractProcessor {
+
+    protected static final String ERROR_REASON_LABEL = "ERROR_REASON";
+    public static final String FRAGMENT_ID = 
FragmentAttributes.FRAGMENT_ID.key();
+    public static final String FRAGMENT_INDEX = 
FragmentAttributes.FRAGMENT_INDEX.key();
+    public static final String FRAGMENT_COUNT = 
FragmentAttributes.FRAGMENT_COUNT.key();
+    public static final String SEGMENT_ORIGINAL_FILENAME = 
FragmentAttributes.SEGMENT_ORIGINAL_FILENAME.key();
+
+    public static final PropertyDescriptor PCAP_MAX_SIZE = new 
PropertyDescriptor
+            .Builder().name("PCAP Max Size")
+            .displayName("PCAP Max Size")
+            .description("Maximum size of each output PCAP file. PCAP packets 
larger than the configured size result in routing FlowFiles to the failure 
relationship.")
+            .required(true)
+            .defaultValue("1 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_ORIGINAL = new Relationship.Builder()
+            .name("original")
+            .description("The original FlowFile that was split into segments. 
If the FlowFile fails processing, nothing will be sent to "
+            + "this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile cannot be transformed from the 
configured input format to the configured output format, "
+            + "the unchanged FlowFile will be routed to this relationship.")
+            .build();
+    public static final Relationship REL_SPLIT = new Relationship.Builder()
+            .name("split")
+            .description("The individual PCAP 'segments' of the original PCAP 
FlowFile will be routed to this relationship.")
+            .build();
+
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
List.of(PCAP_MAX_SIZE);
+    private static final Set<Relationship> RELATIONSHIPS = 
Set.of(REL_ORIGINAL, REL_FAILURE, REL_SPLIT);
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return RELATIONSHIPS;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    /**
+     * This method is called when a trigger event occurs in the processor.
+     * It processes the incoming flow file, splits it into smaller pcap files 
based on the PCAP Max Size,
+     * and transfers the split pcap files to the success relationship.
+     * If the flow file is empty or not parseable, it is transferred to the 
failure relationship.
+     *
+     * @param context  the process context
+     * @param session  the process session
+     */
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) {
+
+        final int pcapMaxSize = 
context.getProperty(PCAP_MAX_SIZE.getName()).asDataSize(DataUnit.B).intValue();
+        FlowFile originalFlowFile = session.get();
+        if (originalFlowFile == null) {
+            return;
+        }
+
+        final List<FlowFile> splitFiles = new ArrayList<>();

Review Comment:
   This does not seem necessary once the callback has a `getSplitFiles()` method
   ```suggestion
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/PCAPHeader.java:
##########
@@ -0,0 +1,95 @@
+// MIT License
+
+// Copyright (c) 2015-2024 Kaitai Project
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to 
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in 
all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
+// SOFTWARE.
+
+package org.apache.nifi.processors.network.pcap;
+
+public class PCAPHeader {
+    static final int PCAP_HEADER_LENGTH = 24;
+    private final ByteBufferReader io;
+    private final byte[] magicNumber;
+    private final int versionMajor;
+    private final int versionMinor;
+    private final int thiszone;
+    private final long sigfigs;
+    private final long snaplen;
+    private final long network;
+
+    public PCAPHeader(ByteBufferReader io) {
+        this.io = io;
+        this.magicNumber = this.io.readBytes(4);
+        this.versionMajor = this.io.readU2();
+        this.versionMinor = this.io.readU2();
+        this.thiszone = this.io.readS4();
+        this.sigfigs = this.io.readU4();
+        this.snaplen = this.io.readU4();
+        this.network = this.io.readU4();
+    }
+
+    public ByteBufferReader io() {
+        return io;
+    }

Review Comment:
   It does not seem as `io `is used anywhere else but the constructor. If that 
is the case remove the instance variable and its getter method `io()`.
   
   ```suggestion
       static final int PCAP_HEADER_LENGTH = 24;
       private final byte[] magicNumber;
       private final int versionMajor;
       private final int versionMinor;
       private final int thiszone;
       private final long sigfigs;
       private final long snaplen;
       private final long network;
   
       public PCAPHeader(ByteBufferReader io) {
           this.magicNumber = io.readBytes(4);
           this.versionMajor = io.readU2();
           this.versionMinor = io.readU2();
           this.thiszone = io.readS4();
           this.sigfigs = io.readU4();
           this.snaplen = io.readU4();
           this.network = io.readU4();
       }
   
   ```
   



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/ByteBufferReader.java:
##########
@@ -0,0 +1,69 @@
+// MIT License
+
+// Copyright (c) 2015-2024 Kaitai Project
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to 
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in 
all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
+// SOFTWARE.
+
+package org.apache.nifi.processors.network.pcap;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+public class ByteBufferReader {
+   final private ByteBuffer buffer;
+
+    public ByteBufferReader(byte[] byteArray) {
+        this.buffer = ByteBuffer.wrap(byteArray);
+    }
+
+    public int readU2() {
+        buffer.order(ByteOrder.LITTLE_ENDIAN);
+        return (buffer.getShort() & 0xffff);
+    }
+
+    public long readU4() {
+        buffer.order(ByteOrder.LITTLE_ENDIAN);
+        return ((long) buffer.getInt() & 0xffffffffL);
+    }
+
+    public int readS4() {
+        buffer.order(ByteOrder.LITTLE_ENDIAN);
+        return buffer.getInt();
+    }
+
+    public byte[] readBytes(int n) {
+        byte[] output = new byte[n];
+        buffer.get(output);
+        return output;
+    }
+
+    public byte[] readBytes(long n) {
+        byte[] output = new byte[(int) n];
+        buffer.get(output);
+        return output;

Review Comment:
   The body is the same as the `readBytes` method which takes an `int` so just 
cast `n` and call the `int` version method.
   ```suggestion
           return readBytes((int) n);
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/SplitPCAP.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.network.pcap;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.flowfile.attributes.FragmentAttributes;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.io.InputStreamCallback;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+@SideEffectFree
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@Tags({"PCAP", "Splitter", "Network", "Packet", "Capture", "Wireshark", 
"TShark", "TcpDump", "WinDump", "sniffers"})
+@CapabilityDescription("Splits one pcap file into multiple pcap files based on 
a maximum size.")
+@WritesAttributes({
+    @WritesAttribute(
+        attribute = SplitPCAP.ERROR_REASON_LABEL,
+        description = "The reason the flowfile was sent to the failure 
relationship."
+    ),
+    @WritesAttribute(
+        attribute = "fragment.identifier",
+        description = "All split PCAP FlowFiles produced from the same parent 
PCAP FlowFile will have the same randomly generated UUID added for this 
attribute"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.index",
+        description = "A one-up number that indicates the ordering of the 
split PCAP FlowFiles that were created from a single parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.count",
+        description = "The number of split PCAP FlowFiles generated from the 
parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "segment.original.filename ",
+        description = "The filename of the parent PCAP FlowFile"
+    )
+})
+
+public class SplitPCAP extends AbstractProcessor {
+
+    protected static final String ERROR_REASON_LABEL = "ERROR_REASON";
+    public static final String FRAGMENT_ID = 
FragmentAttributes.FRAGMENT_ID.key();
+    public static final String FRAGMENT_INDEX = 
FragmentAttributes.FRAGMENT_INDEX.key();
+    public static final String FRAGMENT_COUNT = 
FragmentAttributes.FRAGMENT_COUNT.key();
+    public static final String SEGMENT_ORIGINAL_FILENAME = 
FragmentAttributes.SEGMENT_ORIGINAL_FILENAME.key();
+
+    public static final PropertyDescriptor PCAP_MAX_SIZE = new 
PropertyDescriptor
+            .Builder().name("PCAP Max Size")
+            .displayName("PCAP Max Size")
+            .description("Maximum size of each output PCAP file. PCAP packets 
larger than the configured size result in routing FlowFiles to the failure 
relationship.")
+            .required(true)
+            .defaultValue("1 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_ORIGINAL = new Relationship.Builder()
+            .name("original")
+            .description("The original FlowFile that was split into segments. 
If the FlowFile fails processing, nothing will be sent to "
+            + "this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile cannot be transformed from the 
configured input format to the configured output format, "
+            + "the unchanged FlowFile will be routed to this relationship.")
+            .build();
+    public static final Relationship REL_SPLIT = new Relationship.Builder()
+            .name("split")
+            .description("The individual PCAP 'segments' of the original PCAP 
FlowFile will be routed to this relationship.")
+            .build();
+
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
List.of(PCAP_MAX_SIZE);
+    private static final Set<Relationship> RELATIONSHIPS = 
Set.of(REL_ORIGINAL, REL_FAILURE, REL_SPLIT);
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return RELATIONSHIPS;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    /**
+     * This method is called when a trigger event occurs in the processor.
+     * It processes the incoming flow file, splits it into smaller pcap files 
based on the PCAP Max Size,
+     * and transfers the split pcap files to the success relationship.
+     * If the flow file is empty or not parseable, it is transferred to the 
failure relationship.
+     *
+     * @param context  the process context
+     * @param session  the process session
+     */
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) {
+
+        final int pcapMaxSize = 
context.getProperty(PCAP_MAX_SIZE.getName()).asDataSize(DataUnit.B).intValue();
+        FlowFile originalFlowFile = session.get();
+        if (originalFlowFile == null) {
+            return;
+        }
+
+        final List<FlowFile> splitFiles = new ArrayList<>();
+
+        PCAPStreamSplitterCallback callback = new 
PCAPStreamSplitterCallback(session, originalFlowFile, pcapMaxSize);
+
+        try {
+            session.read(originalFlowFile, callback);
+        } catch (ProcessException e) {
+            splitFiles.addAll(callback.getSplitFiles());
+
+            getLogger().error("Failed to split {}", originalFlowFile, e);
+            session.remove(splitFiles);
+            splitFiles.clear();
+            session.putAttribute(originalFlowFile, ERROR_REASON_LABEL, 
e.getMessage());
+            session.transfer(originalFlowFile, REL_FAILURE);
+            return;
+        }
+
+        splitFiles.addAll(callback.getSplitFiles());
+
+        final String fragmentId = UUID.randomUUID().toString();
+        final String originalFileName = 
originalFlowFile.getAttribute(CoreAttributes.FILENAME.key());
+        final String originalFileNameWithoutExtension = 
originalFileName.substring(0, originalFileName.lastIndexOf("."));
+
+        final Map<String, String> attributes = new HashMap<>();
+        attributes.put(FRAGMENT_COUNT, String.valueOf(splitFiles.size()));
+        attributes.put(FRAGMENT_ID, fragmentId);
+        attributes.put(SEGMENT_ORIGINAL_FILENAME, originalFileName);
+
+        IntStream.range(0, splitFiles.size()).forEach(index -> {
+            FlowFile split = splitFiles.get(index);

Review Comment:
   Per earlier comment. The only reason now to add a variable for `splitFiles` 
is to simplify the `forEach` loop.
   ```suggestion
           final String fragmentId = UUID.randomUUID().toString();
           final String originalFileName = 
originalFlowFile.getAttribute(CoreAttributes.FILENAME.key());
           final String originalFileNameWithoutExtension = 
originalFileName.substring(0, originalFileName.lastIndexOf("."));
   
           final Map<String, String> attributes = new HashMap<>();
           attributes.put(FRAGMENT_COUNT, String.valueOf(splitFiles.size()));
           attributes.put(FRAGMENT_ID, fragmentId);
           attributes.put(SEGMENT_ORIGINAL_FILENAME, originalFileName);
   
           final List<FlowFile> splitFiles = callback.getSplitFiles();
           IntStream.range(0, splitFiles.size()).forEach(index -> {
               FlowFile split = splitFiles.get(index);
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/SplitPCAP.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.network.pcap;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.flowfile.attributes.FragmentAttributes;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.io.InputStreamCallback;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+@SideEffectFree
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@Tags({"PCAP", "Splitter", "Network", "Packet", "Capture", "Wireshark", 
"TShark", "TcpDump", "WinDump", "sniffers"})
+@CapabilityDescription("Splits one pcap file into multiple pcap files based on 
a maximum size.")
+@WritesAttributes({
+    @WritesAttribute(
+        attribute = SplitPCAP.ERROR_REASON_LABEL,
+        description = "The reason the flowfile was sent to the failure 
relationship."
+    ),
+    @WritesAttribute(
+        attribute = "fragment.identifier",
+        description = "All split PCAP FlowFiles produced from the same parent 
PCAP FlowFile will have the same randomly generated UUID added for this 
attribute"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.index",
+        description = "A one-up number that indicates the ordering of the 
split PCAP FlowFiles that were created from a single parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.count",
+        description = "The number of split PCAP FlowFiles generated from the 
parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "segment.original.filename ",
+        description = "The filename of the parent PCAP FlowFile"
+    )
+})
+
+public class SplitPCAP extends AbstractProcessor {
+
+    protected static final String ERROR_REASON_LABEL = "ERROR_REASON";
+    public static final String FRAGMENT_ID = 
FragmentAttributes.FRAGMENT_ID.key();
+    public static final String FRAGMENT_INDEX = 
FragmentAttributes.FRAGMENT_INDEX.key();
+    public static final String FRAGMENT_COUNT = 
FragmentAttributes.FRAGMENT_COUNT.key();
+    public static final String SEGMENT_ORIGINAL_FILENAME = 
FragmentAttributes.SEGMENT_ORIGINAL_FILENAME.key();
+
+    public static final PropertyDescriptor PCAP_MAX_SIZE = new 
PropertyDescriptor
+            .Builder().name("PCAP Max Size")
+            .displayName("PCAP Max Size")
+            .description("Maximum size of each output PCAP file. PCAP packets 
larger than the configured size result in routing FlowFiles to the failure 
relationship.")
+            .required(true)
+            .defaultValue("1 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_ORIGINAL = new Relationship.Builder()
+            .name("original")
+            .description("The original FlowFile that was split into segments. 
If the FlowFile fails processing, nothing will be sent to "
+            + "this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile cannot be transformed from the 
configured input format to the configured output format, "
+            + "the unchanged FlowFile will be routed to this relationship.")
+            .build();
+    public static final Relationship REL_SPLIT = new Relationship.Builder()
+            .name("split")
+            .description("The individual PCAP 'segments' of the original PCAP 
FlowFile will be routed to this relationship.")
+            .build();
+
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
List.of(PCAP_MAX_SIZE);
+    private static final Set<Relationship> RELATIONSHIPS = 
Set.of(REL_ORIGINAL, REL_FAILURE, REL_SPLIT);
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return RELATIONSHIPS;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    /**
+     * This method is called when a trigger event occurs in the processor.
+     * It processes the incoming flow file, splits it into smaller pcap files 
based on the PCAP Max Size,
+     * and transfers the split pcap files to the success relationship.
+     * If the flow file is empty or not parseable, it is transferred to the 
failure relationship.
+     *
+     * @param context  the process context
+     * @param session  the process session
+     */
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) {
+
+        final int pcapMaxSize = 
context.getProperty(PCAP_MAX_SIZE.getName()).asDataSize(DataUnit.B).intValue();
+        FlowFile originalFlowFile = session.get();
+        if (originalFlowFile == null) {
+            return;
+        }
+
+        final List<FlowFile> splitFiles = new ArrayList<>();
+
+        PCAPStreamSplitterCallback callback = new 
PCAPStreamSplitterCallback(session, originalFlowFile, pcapMaxSize);
+
+        try {
+            session.read(originalFlowFile, callback);
+        } catch (ProcessException e) {
+            splitFiles.addAll(callback.getSplitFiles());
+
+            getLogger().error("Failed to split {}", originalFlowFile, e);
+            session.remove(splitFiles);
+            splitFiles.clear();
+            session.putAttribute(originalFlowFile, ERROR_REASON_LABEL, 
e.getMessage());
+            session.transfer(originalFlowFile, REL_FAILURE);
+            return;
+        }
+
+        splitFiles.addAll(callback.getSplitFiles());
+
+        final String fragmentId = UUID.randomUUID().toString();
+        final String originalFileName = 
originalFlowFile.getAttribute(CoreAttributes.FILENAME.key());
+        final String originalFileNameWithoutExtension = 
originalFileName.substring(0, originalFileName.lastIndexOf("."));
+
+        final Map<String, String> attributes = new HashMap<>();
+        attributes.put(FRAGMENT_COUNT, String.valueOf(splitFiles.size()));
+        attributes.put(FRAGMENT_ID, fragmentId);
+        attributes.put(SEGMENT_ORIGINAL_FILENAME, originalFileName);
+
+        IntStream.range(0, splitFiles.size()).forEach(index -> {
+            FlowFile split = splitFiles.get(index);
+            attributes.put(CoreAttributes.FILENAME.key(), 
"%s-%d.pcap".formatted(originalFileNameWithoutExtension, index));
+            attributes.put(FRAGMENT_INDEX, Integer.toString(index));
+            session.transfer(session.putAllAttributes(split, attributes), 
REL_SPLIT);
+        });
+        session.transfer(originalFlowFile, REL_ORIGINAL);
+    }
+
+    protected class PCAPStreamSplitterCallback implements InputStreamCallback {
+        private final ProcessSession session;
+        private final FlowFile originalFlowFile;
+        private final int pcapMaxSize;
+        private final List<FlowFile> splitFiles = new ArrayList<>();
+
+        public List<FlowFile> getSplitFiles() {
+            return splitFiles;
+        }
+
+        public PCAPStreamSplitterCallback( ProcessSession session, FlowFile 
flowFile, int pcapMaxSize) {
+            this.session = session;
+            this.originalFlowFile = flowFile;
+            this.pcapMaxSize = pcapMaxSize;
+        }
+
+        private Packet getNextPacket(BufferedInputStream bufferedStream, PCAP 
templatePcap, int totalPackets) throws IOException {
+            byte[] packetHeader = new byte[Packet.PACKET_HEADER_LENGTH];
+            bufferedStream.read(packetHeader, 0, Packet.PACKET_HEADER_LENGTH);
+            Packet currentPacket = new Packet(packetHeader, templatePcap);
+
+            if (currentPacket.totalLength() > this.pcapMaxSize) {
+                throw new ProcessException("PCAP Packet length [%d] larger 
then configured maximum [%d]".formatted(currentPacket.totalLength(), 
pcapMaxSize));
+            }
+
+            byte[] packetbody = new byte[(int) currentPacket.expectedLength()];
+
+            bufferedStream.read(packetbody, 0, (int) 
currentPacket.expectedLength());
+            currentPacket.setBody(packetbody);
+
+            if (currentPacket.isInvalid()) {
+                throw new ProcessException("PCAP contains an invalid packet. 
Packet number [%d] is invalid - [%s]".formatted(totalPackets, 
currentPacket.invalidityReason()));
+            }
+
+            return currentPacket;
+        }
+
+        @Override
+        public void process(InputStream inStream) throws IOException {
+            final List<Packet> loadedPackets = new ArrayList<>();
+            final BufferedInputStream bufferedStream = new 
BufferedInputStream(inStream);
+            int totalPackets = 1;
+
+            if ( bufferedStream.available() == 0 ) {
+                throw new ProcessException("PCAP file empty.");
+            }
+
+            final byte[] pcapHeader = new byte[PCAPHeader.PCAP_HEADER_LENGTH];
+            bufferedStream.read(pcapHeader, 0, PCAPHeader.PCAP_HEADER_LENGTH);
+            int currentPcapTotalLength = PCAPHeader.PCAP_HEADER_LENGTH;
+
+            final PCAP templatePcap = new PCAP(new 
ByteBufferReader(pcapHeader));
+
+            while ( bufferedStream.available() > 0 ) {

Review Comment:
   ```suggestion
               while (bufferedStream.available() > 0) {
   ```



##########
nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/SplitPCAP.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.network.pcap;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.behavior.WritesAttributes;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.flowfile.attributes.FragmentAttributes;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.io.InputStreamCallback;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+@SideEffectFree
+@InputRequirement(Requirement.INPUT_REQUIRED)
+@Tags({"PCAP", "Splitter", "Network", "Packet", "Capture", "Wireshark", 
"TShark", "TcpDump", "WinDump", "sniffers"})
+@CapabilityDescription("Splits one pcap file into multiple pcap files based on 
a maximum size.")
+@WritesAttributes({
+    @WritesAttribute(
+        attribute = SplitPCAP.ERROR_REASON_LABEL,
+        description = "The reason the flowfile was sent to the failure 
relationship."
+    ),
+    @WritesAttribute(
+        attribute = "fragment.identifier",
+        description = "All split PCAP FlowFiles produced from the same parent 
PCAP FlowFile will have the same randomly generated UUID added for this 
attribute"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.index",
+        description = "A one-up number that indicates the ordering of the 
split PCAP FlowFiles that were created from a single parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "fragment.count",
+        description = "The number of split PCAP FlowFiles generated from the 
parent PCAP FlowFile"
+    ),
+    @WritesAttribute(
+        attribute = "segment.original.filename ",
+        description = "The filename of the parent PCAP FlowFile"
+    )
+})
+
+public class SplitPCAP extends AbstractProcessor {
+
+    protected static final String ERROR_REASON_LABEL = "ERROR_REASON";
+    public static final String FRAGMENT_ID = 
FragmentAttributes.FRAGMENT_ID.key();
+    public static final String FRAGMENT_INDEX = 
FragmentAttributes.FRAGMENT_INDEX.key();
+    public static final String FRAGMENT_COUNT = 
FragmentAttributes.FRAGMENT_COUNT.key();
+    public static final String SEGMENT_ORIGINAL_FILENAME = 
FragmentAttributes.SEGMENT_ORIGINAL_FILENAME.key();
+
+    public static final PropertyDescriptor PCAP_MAX_SIZE = new 
PropertyDescriptor
+            .Builder().name("PCAP Max Size")
+            .displayName("PCAP Max Size")
+            .description("Maximum size of each output PCAP file. PCAP packets 
larger than the configured size result in routing FlowFiles to the failure 
relationship.")
+            .required(true)
+            .defaultValue("1 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_ORIGINAL = new Relationship.Builder()
+            .name("original")
+            .description("The original FlowFile that was split into segments. 
If the FlowFile fails processing, nothing will be sent to "
+            + "this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile cannot be transformed from the 
configured input format to the configured output format, "
+            + "the unchanged FlowFile will be routed to this relationship.")
+            .build();
+    public static final Relationship REL_SPLIT = new Relationship.Builder()
+            .name("split")
+            .description("The individual PCAP 'segments' of the original PCAP 
FlowFile will be routed to this relationship.")
+            .build();
+
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
List.of(PCAP_MAX_SIZE);
+    private static final Set<Relationship> RELATIONSHIPS = 
Set.of(REL_ORIGINAL, REL_FAILURE, REL_SPLIT);
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return RELATIONSHIPS;
+    }
+
+    @Override
+    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    /**
+     * This method is called when a trigger event occurs in the processor.
+     * It processes the incoming flow file, splits it into smaller pcap files 
based on the PCAP Max Size,
+     * and transfers the split pcap files to the success relationship.
+     * If the flow file is empty or not parseable, it is transferred to the 
failure relationship.
+     *
+     * @param context  the process context
+     * @param session  the process session
+     */
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) {
+
+        final int pcapMaxSize = 
context.getProperty(PCAP_MAX_SIZE.getName()).asDataSize(DataUnit.B).intValue();
+        FlowFile originalFlowFile = session.get();
+        if (originalFlowFile == null) {
+            return;
+        }
+
+        final List<FlowFile> splitFiles = new ArrayList<>();
+
+        PCAPStreamSplitterCallback callback = new 
PCAPStreamSplitterCallback(session, originalFlowFile, pcapMaxSize);

Review Comment:
   ```suggestion
           final PCAPStreamSplitterCallback callback = new 
PCAPStreamSplitterCallback(session, originalFlowFile, pcapMaxSize);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to