File tree Expand file tree Collapse file tree 4 files changed +62
-1
lines changed
src/main/java/org/perlonjava/operators Expand file tree Collapse file tree 4 files changed +62
-1
lines changed Original file line number Diff line number Diff line change 11package org .perlonjava .operators ;
22
33import org .perlonjava .runtime .*;
4+ import org .perlonjava .operators .pack .*;
45
56import java .io .ByteArrayOutputStream ;
67import java .io .IOException ;
78import java .nio .charset .StandardCharsets ;
89import java .util .List ;
910import java .util .Map ;
1011import java .util .HashMap ;
11-
1212/**
1313 * Provides functionality to pack a list of scalars into a binary string
1414 * based on a specified template, similar to Perl's pack function.
Original file line number Diff line number Diff line change 1+ package org .perlonjava .operators .pack ;
2+
3+ import org .perlonjava .runtime .RuntimeScalar ;
4+ import java .io .ByteArrayOutputStream ;
5+ import java .util .List ;
6+
7+ /**
8+ * Handles 'x' format - null padding
9+ */
10+ public class NullPadHandler implements PackFormatHandler {
11+ @ Override
12+ public int pack (ByteArrayOutputStream output , List <RuntimeScalar > values ,
13+ int valueIndex , int count , PackModifiers modifiers ) {
14+ // 'x' doesn't consume any values, just writes null bytes
15+ for (int j = 0 ; j < count ; j ++) {
16+ output .write (0 );
17+ }
18+ return 0 ; // consumes no values
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package org .perlonjava .operators .pack ;
2+
3+ import org .perlonjava .runtime .RuntimeScalar ;
4+ import java .io .ByteArrayOutputStream ;
5+ import java .util .List ;
6+
7+ /**
8+ * Interface for handling pack format characters
9+ */
10+ public interface PackFormatHandler {
11+ /**
12+ * Pack values according to this format
13+ *
14+ * @param output The output stream to write to
15+ * @param values The list of values to pack
16+ * @param valueIndex Current position in values list
17+ * @param count Repeat count (Integer.MAX_VALUE for *)
18+ * @param modifiers Format modifiers (endianness, etc.)
19+ * @return number of values consumed from the values list
20+ */
21+ int pack (ByteArrayOutputStream output , List <RuntimeScalar > values ,
22+ int valueIndex , int count , PackModifiers modifiers );
23+ }
Original file line number Diff line number Diff line change 1+ package org .perlonjava .operators .pack ;
2+
3+ /**
4+ * Holds format modifiers like endianness
5+ */
6+ public class PackModifiers {
7+ public final boolean bigEndian ;
8+ public final boolean littleEndian ;
9+ public final boolean nativeSize ;
10+
11+ public PackModifiers (boolean bigEndian , boolean littleEndian , boolean nativeSize ) {
12+ this .bigEndian = bigEndian ;
13+ this .littleEndian = littleEndian ;
14+ this .nativeSize = nativeSize ;
15+ }
16+
17+ public static PackModifiers DEFAULT = new PackModifiers (false , false , false );
18+ }
You can’t perform that action at this time.
0 commit comments