();
99 |
100 | byte[] array = null;
101 | int i = 0;
102 | int packetCounter = 0;
103 |
104 | for (byte b : readAllLines) {
105 |
106 | if (i == 0) {
107 | array = new byte[PACKETSIZE];
108 | }
109 |
110 | array[i] = b;
111 |
112 | i++;
113 | if (i == PACKETSIZE) {
114 | String crc32Checksum = calcChecksum(array);
115 | array = Util.concatenateArrays(generatePrintPrePayload(packetCounter, i, crc32Checksum, false), array);
116 | packetCounter++;
117 | gcode.add(array);
118 | i = 0;
119 | }
120 | }
121 | String crc32Checksum = calcChecksum(Arrays.copyOfRange(array, 0, i));
122 | array = Util.concatenateArrays(generatePrintPrePayload(packetCounter, i, crc32Checksum, true), array);
123 | gcode.add(array);
124 | return gcode;
125 | }
126 | }
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/BinaryDecoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Defines common decoding methods for byte array decoders.
22 | *
23 | */
24 | public interface BinaryDecoder extends Decoder {
25 |
26 | /**
27 | * Decodes a byte array and returns the results as a byte array.
28 | *
29 | * @param source
30 | * A byte array which has been encoded with the appropriate encoder
31 | * @return a byte array that contains decoded content
32 | * @throws DecoderException
33 | * A decoder exception is thrown if a Decoder encounters a failure condition during the decode process.
34 | */
35 | byte[] decode(byte[] source) throws DecoderException;
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/BinaryEncoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Defines common encoding methods for byte array encoders.
22 | *
23 | */
24 | public interface BinaryEncoder extends Encoder {
25 |
26 | /**
27 | * Encodes a byte array and return the encoded data as a byte array.
28 | *
29 | * @param source
30 | * Data to be encoded
31 | * @return A byte array containing the encoded data
32 | * @throws EncoderException
33 | * thrown if the Encoder encounters a failure condition during the encoding process.
34 | */
35 | byte[] encode(byte[] source) throws EncoderException;
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/CharEncoding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Character encoding names required of every implementation of the Java platform.
22 | *
23 | * From the Java documentation Standard charsets:
25 | *
26 | * Every implementation of the Java platform is required to support the following character encodings. Consult the
27 | * release documentation for your implementation to see if any other encodings are supported. Consult the release
28 | * documentation for your implementation to see if any other encodings are supported.
29 | *
30 | *
31 | *
32 | * - {@code US-ASCII}
33 | * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.
34 | * - {@code ISO-8859-1}
35 | * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
36 | * - {@code UTF-8}
37 | * Eight-bit Unicode Transformation Format.
38 | * - {@code UTF-16BE}
39 | * Sixteen-bit Unicode Transformation Format, big-endian byte order.
40 | * - {@code UTF-16LE}
41 | * Sixteen-bit Unicode Transformation Format, little-endian byte order.
42 | * - {@code UTF-16}
43 | * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
44 | * accepted on input, big-endian used on output.)
45 | *
46 | *
47 | * This perhaps would best belong in the [lang] project. Even if a similar interface is defined in [lang], it is not
48 | * foreseen that [codec] would be made to depend on [lang].
49 | *
50 | *
51 | * This class is immutable and thread-safe.
52 | *
53 | *
54 | * @see Standard charsets
55 | * @since 1.4
56 | */
57 | public class CharEncoding {
58 |
59 | /**
60 | * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
61 | *
62 | * Every implementation of the Java platform is required to support this character encoding.
63 | *
64 | *
65 | * @see Standard charsets
66 | */
67 | public static final String ISO_8859_1 = "ISO-8859-1";
68 |
69 | /**
70 | * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
71 | *
72 | * Every implementation of the Java platform is required to support this character encoding.
73 | *
74 | *
75 | * @see Standard charsets
76 | */
77 | public static final String US_ASCII = "US-ASCII";
78 |
79 | /**
80 | * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
81 | * (either order accepted on input, big-endian used on output)
82 | *
83 | * Every implementation of the Java platform is required to support this character encoding.
84 | *
85 | *
86 | * @see Standard charsets
87 | */
88 | public static final String UTF_16 = "UTF-16";
89 |
90 | /**
91 | * Sixteen-bit Unicode Transformation Format, big-endian byte order.
92 | *
93 | * Every implementation of the Java platform is required to support this character encoding.
94 | *
95 | *
96 | * @see Standard charsets
97 | */
98 | public static final String UTF_16BE = "UTF-16BE";
99 |
100 | /**
101 | * Sixteen-bit Unicode Transformation Format, little-endian byte order.
102 | *
103 | * Every implementation of the Java platform is required to support this character encoding.
104 | *
105 | *
106 | * @see Standard charsets
107 | */
108 | public static final String UTF_16LE = "UTF-16LE";
109 |
110 | /**
111 | * Eight-bit Unicode Transformation Format.
112 | *
113 | * Every implementation of the Java platform is required to support this character encoding.
114 | *
115 | *
116 | * @see Standard charsets
117 | */
118 | public static final String UTF_8 = "UTF-8";
119 | }
120 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/Decoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Provides the highest level of abstraction for Decoders.
22 | *
23 | * This is the sister interface of {@link Encoder}. All Decoders implement this common generic interface.
24 | * Allows a user to pass a generic Object to any Decoder implementation in the codec package.
25 | *
26 | * One of the two interfaces at the center of the codec package.
27 | *
28 | */
29 | public interface Decoder {
30 |
31 | /**
32 | * Decodes an "encoded" Object and returns a "decoded" Object. Note that the implementation of this interface will
33 | * try to cast the Object parameter to the specific type expected by a particular Decoder implementation. If a
34 | * {@link ClassCastException} occurs this decode method will throw a DecoderException.
35 | *
36 | * @param source
37 | * the object to decode
38 | * @return a 'decoded" object
39 | * @throws DecoderException
40 | * a decoder exception can be thrown for any number of reasons. Some good candidates are that the
41 | * parameter passed to this method is null, a param cannot be cast to the appropriate type for a
42 | * specific encoder.
43 | */
44 | Object decode(Object source) throws DecoderException;
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/DecoderException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Thrown when there is a failure condition during the decoding process. This exception is thrown when a {@link Decoder}
22 | * encounters a decoding specific exception such as invalid data, or characters outside of the expected range.
23 | *
24 | */
25 | public class DecoderException extends Exception {
26 |
27 | /**
28 | * Declares the Serial Version Uid.
29 | *
30 | * @see Always Declare Serial Version Uid
31 | */
32 | private static final long serialVersionUID = 1L;
33 |
34 | /**
35 | * Constructs a new exception with {@code null} as its detail message. The cause is not initialized, and may
36 | * subsequently be initialized by a call to {@link #initCause}.
37 | *
38 | * @since 1.4
39 | */
40 | public DecoderException() {
41 | super();
42 | }
43 |
44 | /**
45 | * Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently
46 | * be initialized by a call to {@link #initCause}.
47 | *
48 | * @param message
49 | * The detail message which is saved for later retrieval by the {@link #getMessage()} method.
50 | */
51 | public DecoderException(final String message) {
52 | super(message);
53 | }
54 |
55 | /**
56 | * Constructs a new exception with the specified detail message and cause.
57 | *
58 | * Note that the detail message associated with {@code cause} is not automatically incorporated into this
59 | * exception's detail message.
60 | *
61 | * @param message
62 | * The detail message which is saved for later retrieval by the {@link #getMessage()} method.
63 | * @param cause
64 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null}
65 | * value is permitted, and indicates that the cause is nonexistent or unknown.
66 | * @since 1.4
67 | */
68 | public DecoderException(final String message, final Throwable cause) {
69 | super(message, cause);
70 | }
71 |
72 | /**
73 | * Constructs a new exception with the specified cause and a detail message of (cause==null ?
74 | * null : cause.toString())
(which typically contains the class and detail message of {@code cause}).
75 | * This constructor is useful for exceptions that are little more than wrappers for other throwables.
76 | *
77 | * @param cause
78 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null}
79 | * value is permitted, and indicates that the cause is nonexistent or unknown.
80 | * @since 1.4
81 | */
82 | public DecoderException(final Throwable cause) {
83 | super(cause);
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/Encoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Provides the highest level of abstraction for Encoders.
22 | *
23 | * This is the sister interface of {@link Decoder}. Every implementation of Encoder provides this
24 | * common generic interface which allows a user to pass a generic Object to any Encoder implementation
25 | * in the codec package.
26 | *
27 | */
28 | public interface Encoder {
29 |
30 | /**
31 | * Encodes an "Object" and returns the encoded content as an Object. The Objects here may just be
32 | * {@code byte[]} or {@code String}s depending on the implementation used.
33 | *
34 | * @param source
35 | * An object to encode
36 | * @return An "encoded" Object
37 | * @throws EncoderException
38 | * An encoder exception is thrown if the encoder experiences a failure condition during the encoding
39 | * process.
40 | */
41 | Object encode(Object source) throws EncoderException;
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/EncoderException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Thrown when there is a failure condition during the encoding process. This exception is thrown when an
22 | * {@link Encoder} encounters a encoding specific exception such as invalid data, inability to calculate a checksum,
23 | * characters outside of the expected range.
24 | *
25 | */
26 | public class EncoderException extends Exception {
27 |
28 | /**
29 | * Declares the Serial Version Uid.
30 | *
31 | * @see Always Declare Serial Version Uid
32 | */
33 | private static final long serialVersionUID = 1L;
34 |
35 | /**
36 | * Constructs a new exception with {@code null} as its detail message. The cause is not initialized, and may
37 | * subsequently be initialized by a call to {@link #initCause}.
38 | *
39 | * @since 1.4
40 | */
41 | public EncoderException() {
42 | super();
43 | }
44 |
45 | /**
46 | * Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently
47 | * be initialized by a call to {@link #initCause}.
48 | *
49 | * @param message
50 | * a useful message relating to the encoder specific error.
51 | */
52 | public EncoderException(final String message) {
53 | super(message);
54 | }
55 |
56 | /**
57 | * Constructs a new exception with the specified detail message and cause.
58 | *
59 | *
60 | * Note that the detail message associated with {@code cause} is not automatically incorporated into this
61 | * exception's detail message.
62 | *
63 | *
64 | * @param message
65 | * The detail message which is saved for later retrieval by the {@link #getMessage()} method.
66 | * @param cause
67 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null}
68 | * value is permitted, and indicates that the cause is nonexistent or unknown.
69 | * @since 1.4
70 | */
71 | public EncoderException(final String message, final Throwable cause) {
72 | super(message, cause);
73 | }
74 |
75 | /**
76 | * Constructs a new exception with the specified cause and a detail message of (cause==null ?
77 | * null : cause.toString())
(which typically contains the class and detail message of {@code cause}).
78 | * This constructor is useful for exceptions that are little more than wrappers for other throwables.
79 | *
80 | * @param cause
81 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null}
82 | * value is permitted, and indicates that the cause is nonexistent or unknown.
83 | * @since 1.4
84 | */
85 | public EncoderException(final Throwable cause) {
86 | super(cause);
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/StringDecoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Defines common decoding methods for String decoders.
22 | *
23 | */
24 | public interface StringDecoder extends Decoder {
25 |
26 | /**
27 | * Decodes a String and returns a String.
28 | *
29 | * @param source
30 | * the String to decode
31 | * @return the encoded String
32 | * @throws DecoderException
33 | * thrown if there is an error condition during the Encoding process.
34 | */
35 | String decode(String source) throws DecoderException;
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/StringEncoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec;
19 |
20 | /**
21 | * Defines common encoding methods for String encoders.
22 | *
23 | */
24 | public interface StringEncoder extends Encoder {
25 |
26 | /**
27 | * Encodes a String and returns a String.
28 | *
29 | * @param source
30 | * the String to encode
31 | * @return the encoded String
32 | * @throws EncoderException
33 | * thrown if there is an error condition during the encoding process.
34 | */
35 | String encode(String source) throws EncoderException;
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/commons/codec/binary/Hex.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.codec.binary;
19 |
20 | import java.nio.ByteBuffer;
21 | import java.nio.charset.Charset;
22 | import java.nio.charset.StandardCharsets;
23 |
24 | import org.apache.commons.codec.BinaryDecoder;
25 | import org.apache.commons.codec.BinaryEncoder;
26 | import org.apache.commons.codec.CharEncoding;
27 | import org.apache.commons.codec.DecoderException;
28 | import org.apache.commons.codec.EncoderException;
29 |
30 | /**
31 | * Converts hexadecimal Strings. The Charset used for certain operation can be set, the default is set in
32 | * {@link #DEFAULT_CHARSET_NAME}
33 | *
34 | * This class is thread-safe.
35 | *
36 | * @since 1.1
37 | */
38 | public class Hex implements BinaryEncoder, BinaryDecoder {
39 |
40 | /**
41 | * Default charset is {@link StandardCharsets#UTF_8}.
42 | *
43 | * @since 1.7
44 | */
45 | public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
46 |
47 | /**
48 | * Default charset name is {@link CharEncoding#UTF_8}.
49 | *
50 | * @since 1.4
51 | */
52 | public static final String DEFAULT_CHARSET_NAME = CharEncoding.UTF_8;
53 |
54 | /**
55 | * Used to build output as hex.
56 | */
57 | private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
58 | 'e', 'f' };
59 |
60 | /**
61 | * Used to build output as hex.
62 | */
63 | private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
64 | 'E', 'F' };
65 |
66 | /**
67 | * Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The
68 | * returned array will be half the length of the passed array, as it takes two characters to represent any given
69 | * byte. An exception is thrown if the passed char array has an odd number of elements.
70 | *
71 | * @param data An array of characters containing hexadecimal digits
72 | * @return A byte array containing binary data decoded from the supplied char array.
73 | * @throws DecoderException Thrown if an odd number of characters or illegal characters are supplied
74 | */
75 | public static byte[] decodeHex(final char[] data) throws DecoderException {
76 | final byte[] out = new byte[data.length >> 1];
77 | decodeHex(data, out, 0);
78 | return out;
79 | }
80 |
81 | /**
82 | * Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The
83 | * returned array will be half the length of the passed array, as it takes two characters to represent any given
84 | * byte. An exception is thrown if the passed char array has an odd number of elements.
85 | *
86 | * @param data An array of characters containing hexadecimal digits
87 | * @param out A byte array to contain the binary data decoded from the supplied char array.
88 | * @param outOffset The position within {@code out} to start writing the decoded bytes.
89 | * @return the number of bytes written to {@code out}.
90 | * @throws DecoderException Thrown if an odd number of characters or illegal characters are supplied
91 | * @since 1.15
92 | */
93 | public static int decodeHex(final char[] data, final byte[] out, final int outOffset) throws DecoderException {
94 | final int len = data.length;
95 |
96 | if ((len & 0x01) != 0) {
97 | throw new DecoderException("Odd number of characters.");
98 | }
99 |
100 | final int outLen = len >> 1;
101 | if (out.length - outOffset < outLen) {
102 | throw new DecoderException("Output array is not large enough to accommodate decoded data.");
103 | }
104 |
105 | // two characters form the hex value.
106 | for (int i = outOffset, j = 0; j < len; i++) {
107 | int f = toDigit(data[j], j) << 4;
108 | j++;
109 | f = f | toDigit(data[j], j);
110 | j++;
111 | out[i] = (byte) (f & 0xFF);
112 | }
113 |
114 | return outLen;
115 | }
116 |
117 | /**
118 | * Converts a String representing hexadecimal values into an array of bytes of those same values. The returned array
119 | * will be half the length of the passed String, as it takes two characters to represent any given byte. An
120 | * exception is thrown if the passed String has an odd number of elements.
121 | *
122 | * @param data A String containing hexadecimal digits
123 | * @return A byte array containing binary data decoded from the supplied char array.
124 | * @throws DecoderException Thrown if an odd number of characters or illegal characters are supplied
125 | * @since 1.11
126 | */
127 | public static byte[] decodeHex(final String data) throws DecoderException {
128 | return decodeHex(data.toCharArray());
129 | }
130 |
131 | /**
132 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
133 | * The returned array will be double the length of the passed array, as it takes two characters to represent any
134 | * given byte.
135 | *
136 | * @param data a byte[] to convert to hex characters
137 | * @return A char[] containing lower-case hexadecimal characters
138 | */
139 | public static char[] encodeHex(final byte[] data) {
140 | return encodeHex(data, true);
141 | }
142 |
143 | /**
144 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
145 | * The returned array will be double the length of the passed array, as it takes two characters to represent any
146 | * given byte.
147 | *
148 | * @param data a byte[] to convert to Hex characters
149 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase
150 | * @return A char[] containing hexadecimal characters in the selected case
151 | * @since 1.4
152 | */
153 | public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
154 | return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
155 | }
156 |
157 | /**
158 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
159 | * The returned array will be double the length of the passed array, as it takes two characters to represent any
160 | * given byte.
161 | *
162 | * @param data a byte[] to convert to hex characters
163 | * @param toDigits the output alphabet (must contain at least 16 chars)
164 | * @return A char[] containing the appropriate characters from the alphabet For best results, this should be either
165 | * upper- or lower-case hex.
166 | * @since 1.4
167 | */
168 | protected static char[] encodeHex(final byte[] data, final char[] toDigits) {
169 | final int l = data.length;
170 | final char[] out = new char[l << 1];
171 | encodeHex(data, 0, data.length, toDigits, out, 0);
172 | return out;
173 | }
174 |
175 | /**
176 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
177 | *
178 | * @param data a byte[] to convert to hex characters
179 | * @param dataOffset the position in {@code data} to start encoding from
180 | * @param dataLen the number of bytes from {@code dataOffset} to encode
181 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase
182 | * @return A char[] containing the appropriate characters from the alphabet For best results, this should be either
183 | * upper- or lower-case hex.
184 | * @since 1.15
185 | */
186 | public static char[] encodeHex(final byte[] data, final int dataOffset, final int dataLen,
187 | final boolean toLowerCase) {
188 | final char[] out = new char[dataLen << 1];
189 | encodeHex(data, dataOffset, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER, out, 0);
190 | return out;
191 | }
192 |
193 | /**
194 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
195 | *
196 | * @param data a byte[] to convert to hex characters
197 | * @param dataOffset the position in {@code data} to start encoding from
198 | * @param dataLen the number of bytes from {@code dataOffset} to encode
199 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase
200 | * @param out a char[] which will hold the resultant appropriate characters from the alphabet.
201 | * @param outOffset the position within {@code out} at which to start writing the encoded characters.
202 | * @since 1.15
203 | */
204 | public static void encodeHex(final byte[] data, final int dataOffset, final int dataLen,
205 | final boolean toLowerCase, final char[] out, final int outOffset) {
206 | encodeHex(data, dataOffset, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER, out, outOffset);
207 | }
208 |
209 | /**
210 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
211 | *
212 | * @param data a byte[] to convert to hex characters
213 | * @param dataOffset the position in {@code data} to start encoding from
214 | * @param dataLen the number of bytes from {@code dataOffset} to encode
215 | * @param toDigits the output alphabet (must contain at least 16 chars)
216 | * @param out a char[] which will hold the resultant appropriate characters from the alphabet.
217 | * @param outOffset the position within {@code out} at which to start writing the encoded characters.
218 | */
219 | private static void encodeHex(final byte[] data, final int dataOffset, final int dataLen, final char[] toDigits,
220 | final char[] out, final int outOffset) {
221 | // two characters form the hex value.
222 | for (int i = dataOffset, j = outOffset; i < dataOffset + dataLen; i++) {
223 | out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
224 | out[j++] = toDigits[0x0F & data[i]];
225 | }
226 | }
227 |
228 | /**
229 | * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order. The
230 | * returned array will be double the length of the passed array, as it takes two characters to represent any given
231 | * byte.
232 | *
233 | * All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method
234 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.
235 | *
236 | * @param data a byte buffer to convert to hex characters
237 | * @return A char[] containing lower-case hexadecimal characters
238 | * @since 1.11
239 | */
240 | public static char[] encodeHex(final ByteBuffer data) {
241 | return encodeHex(data, true);
242 | }
243 |
244 | /**
245 | * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order. The
246 | * returned array will be double the length of the passed array, as it takes two characters to represent any given
247 | * byte.
248 | *
249 | * All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method
250 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.
251 | *
252 | * @param data a byte buffer to convert to hex characters
253 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase
254 | * @return A char[] containing hexadecimal characters in the selected case
255 | * @since 1.11
256 | */
257 | public static char[] encodeHex(final ByteBuffer data, final boolean toLowerCase) {
258 | return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
259 | }
260 |
261 | /**
262 | * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order. The
263 | * returned array will be double the length of the passed array, as it takes two characters to represent any given
264 | * byte.
265 | *
266 | * All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method
267 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.
268 | *
269 | * @param byteBuffer a byte buffer to convert to hex characters
270 | * @param toDigits the output alphabet (must be at least 16 characters)
271 | * @return A char[] containing the appropriate characters from the alphabet For best results, this should be either
272 | * upper- or lower-case hex.
273 | * @since 1.11
274 | */
275 | protected static char[] encodeHex(final ByteBuffer byteBuffer, final char[] toDigits) {
276 | return encodeHex(toByteArray(byteBuffer), toDigits);
277 | }
278 |
279 | /**
280 | * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
281 | * String will be double the length of the passed array, as it takes two characters to represent any given byte.
282 | *
283 | * @param data a byte[] to convert to hex characters
284 | * @return A String containing lower-case hexadecimal characters
285 | * @since 1.4
286 | */
287 | public static String encodeHexString(final byte[] data) {
288 | return new String(encodeHex(data));
289 | }
290 |
291 | /**
292 | * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
293 | * String will be double the length of the passed array, as it takes two characters to represent any given byte.
294 | *
295 | * @param data a byte[] to convert to hex characters
296 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase
297 | * @return A String containing lower-case hexadecimal characters
298 | * @since 1.11
299 | */
300 | public static String encodeHexString(final byte[] data, final boolean toLowerCase) {
301 | return new String(encodeHex(data, toLowerCase));
302 | }
303 |
304 | /**
305 | * Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned
306 | * String will be double the length of the passed array, as it takes two characters to represent any given byte.
307 | *
308 | * All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method
309 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.
310 | *
311 | * @param data a byte buffer to convert to hex characters
312 | * @return A String containing lower-case hexadecimal characters
313 | * @since 1.11
314 | */
315 | public static String encodeHexString(final ByteBuffer data) {
316 | return new String(encodeHex(data));
317 | }
318 |
319 | /**
320 | * Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned
321 | * String will be double the length of the passed array, as it takes two characters to represent any given byte.
322 | *
323 | * All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method
324 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.
325 | *
326 | * @param data a byte buffer to convert to hex characters
327 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase
328 | * @return A String containing lower-case hexadecimal characters
329 | * @since 1.11
330 | */
331 | public static String encodeHexString(final ByteBuffer data, final boolean toLowerCase) {
332 | return new String(encodeHex(data, toLowerCase));
333 | }
334 |
335 | /**
336 | * Convert the byte buffer to a byte array. All bytes identified by
337 | * {@link ByteBuffer#remaining()} will be used.
338 | *
339 | * @param byteBuffer the byte buffer
340 | * @return the byte[]
341 | */
342 | private static byte[] toByteArray(final ByteBuffer byteBuffer) {
343 | final int remaining = byteBuffer.remaining();
344 | // Use the underlying buffer if possible
345 | if (byteBuffer.hasArray()) {
346 | final byte[] byteArray = byteBuffer.array();
347 | if (remaining == byteArray.length) {
348 | byteBuffer.position(remaining);
349 | return byteArray;
350 | }
351 | }
352 | // Copy the bytes
353 | final byte[] byteArray = new byte[remaining];
354 | byteBuffer.get(byteArray);
355 | return byteArray;
356 | }
357 |
358 | /**
359 | * Converts a hexadecimal character to an integer.
360 | *
361 | * @param ch A character to convert to an integer digit
362 | * @param index The index of the character in the source
363 | * @return An integer
364 | * @throws DecoderException Thrown if ch is an illegal hex character
365 | */
366 | protected static int toDigit(final char ch, final int index) throws DecoderException {
367 | final int digit = Character.digit(ch, 16);
368 | if (digit == -1) {
369 | throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index);
370 | }
371 | return digit;
372 | }
373 |
374 | private final Charset charset;
375 |
376 | /**
377 | * Creates a new codec with the default charset name {@link #DEFAULT_CHARSET}
378 | */
379 | public Hex() {
380 | // use default encoding
381 | this.charset = DEFAULT_CHARSET;
382 | }
383 |
384 | /**
385 | * Creates a new codec with the given Charset.
386 | *
387 | * @param charset the charset.
388 | * @since 1.7
389 | */
390 | public Hex(final Charset charset) {
391 | this.charset = charset;
392 | }
393 |
394 | /**
395 | * Creates a new codec with the given charset name.
396 | *
397 | * @param charsetName the charset name.
398 | * @throws java.nio.charset.UnsupportedCharsetException If the named charset is unavailable
399 | * @since 1.4
400 | * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable
401 | */
402 | public Hex(final String charsetName) {
403 | this(Charset.forName(charsetName));
404 | }
405 |
406 | /**
407 | * Converts an array of character bytes representing hexadecimal values into an array of bytes of those same values.
408 | * The returned array will be half the length of the passed array, as it takes two characters to represent any given
409 | * byte. An exception is thrown if the passed char array has an odd number of elements.
410 | *
411 | * @param array An array of character bytes containing hexadecimal digits
412 | * @return A byte array containing binary data decoded from the supplied byte array (representing characters).
413 | * @throws DecoderException Thrown if an odd number of characters is supplied to this function
414 | * @see #decodeHex(char[])
415 | */
416 | @Override
417 | public byte[] decode(final byte[] array) throws DecoderException {
418 | return decodeHex(new String(array, getCharset()).toCharArray());
419 | }
420 |
421 | /**
422 | * Converts a buffer of character bytes representing hexadecimal values into an array of bytes of those same values.
423 | * The returned array will be half the length of the passed array, as it takes two characters to represent any given
424 | * byte. An exception is thrown if the passed char array has an odd number of elements.
425 | *
426 | * All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method
427 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.
428 | *
429 | * @param buffer An array of character bytes containing hexadecimal digits
430 | * @return A byte array containing binary data decoded from the supplied byte array (representing characters).
431 | * @throws DecoderException Thrown if an odd number of characters is supplied to this function
432 | * @see #decodeHex(char[])
433 | * @since 1.11
434 | */
435 | public byte[] decode(final ByteBuffer buffer) throws DecoderException {
436 | return decodeHex(new String(toByteArray(buffer), getCharset()).toCharArray());
437 | }
438 |
439 | /**
440 | * Converts a String or an array of character bytes representing hexadecimal values into an array of bytes of those
441 | * same values. The returned array will be half the length of the passed String or array, as it takes two characters
442 | * to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
443 | *
444 | * @param object A String, ByteBuffer, byte[], or an array of character bytes containing hexadecimal digits
445 | * @return A byte array containing binary data decoded from the supplied byte array (representing characters).
446 | * @throws DecoderException Thrown if an odd number of characters is supplied to this function or the object is not
447 | * a String or char[]
448 | * @see #decodeHex(char[])
449 | */
450 | @Override
451 | public Object decode(final Object object) throws DecoderException {
452 | if (object instanceof String) {
453 | return decode(((String) object).toCharArray());
454 | } else if (object instanceof byte[]) {
455 | return decode((byte[]) object);
456 | } else if (object instanceof ByteBuffer) {
457 | return decode((ByteBuffer) object);
458 | } else {
459 | try {
460 | return decodeHex((char[]) object);
461 | } catch (final ClassCastException e) {
462 | throw new DecoderException(e.getMessage(), e);
463 | }
464 | }
465 | }
466 |
467 | /**
468 | * Converts an array of bytes into an array of bytes for the characters representing the hexadecimal values of each
469 | * byte in order. The returned array will be double the length of the passed array, as it takes two characters to
470 | * represent any given byte.
471 | *
472 | * The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
473 | * {@link #getCharset()}.
474 | *
475 | *
476 | * @param array a byte[] to convert to hex characters
477 | * @return A byte[] containing the bytes of the lower-case hexadecimal characters
478 | * @since 1.7 No longer throws IllegalStateException if the charsetName is invalid.
479 | * @see #encodeHex(byte[])
480 | */
481 | @Override
482 | public byte[] encode(final byte[] array) {
483 | return encodeHexString(array).getBytes(this.getCharset());
484 | }
485 |
486 | /**
487 | * Converts byte buffer into an array of bytes for the characters representing the hexadecimal values of each byte
488 | * in order. The returned array will be double the length of the passed array, as it takes two characters to
489 | * represent any given byte.
490 | *
491 | * The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
492 | * {@link #getCharset()}.
493 | *
494 | * All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method
495 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.
496 | *
497 | * @param array a byte buffer to convert to hex characters
498 | * @return A byte[] containing the bytes of the lower-case hexadecimal characters
499 | * @see #encodeHex(byte[])
500 | * @since 1.11
501 | */
502 | public byte[] encode(final ByteBuffer array) {
503 | return encodeHexString(array).getBytes(this.getCharset());
504 | }
505 |
506 | /**
507 | * Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each
508 | * byte in order. The returned array will be double the length of the passed String or array, as it takes two
509 | * characters to represent any given byte.
510 | *
511 | * The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by
512 | * {@link #getCharset()}.
513 | *
514 | *
515 | * @param object a String, ByteBuffer, or byte[] to convert to hex characters
516 | * @return A char[] containing lower-case hexadecimal characters
517 | * @throws EncoderException Thrown if the given object is not a String or byte[]
518 | * @see #encodeHex(byte[])
519 | */
520 | @Override
521 | public Object encode(final Object object) throws EncoderException {
522 | byte[] byteArray;
523 | if (object instanceof String) {
524 | byteArray = ((String) object).getBytes(this.getCharset());
525 | } else if (object instanceof ByteBuffer) {
526 | byteArray = toByteArray((ByteBuffer) object);
527 | } else {
528 | try {
529 | byteArray = (byte[]) object;
530 | } catch (final ClassCastException e) {
531 | throw new EncoderException(e.getMessage(), e);
532 | }
533 | }
534 | return encodeHex(byteArray);
535 | }
536 |
537 | /**
538 | * Gets the charset.
539 | *
540 | * @return the charset.
541 | * @since 1.7
542 | */
543 | public Charset getCharset() {
544 | return this.charset;
545 | }
546 |
547 | /**
548 | * Gets the charset name.
549 | *
550 | * @return the charset name.
551 | * @since 1.4
552 | */
553 | public String getCharsetName() {
554 | return this.charset.name();
555 | }
556 |
557 | /**
558 | * Returns a string representation of the object, which includes the charset name.
559 | *
560 | * @return a string representation of the object.
561 | */
562 | @Override
563 | public String toString() {
564 | return super.toString() + "[charsetName=" + this.charset + "]";
565 | }
566 | }
567 |
--------------------------------------------------------------------------------
/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | %d{dd.MM.yyyy HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{50}.%M - %msg%n
8 |
9 |
10 |
11 |
12 | data/bot.log
13 |
14 |
15 | %d{dd.MM.yyyy HH:mm:ss.SSS} [%thread] %level %logger{50}.%M - %msg%n
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/test/java/de/slg/ddnss/printertool/clients/AdventurerClientTest.java:
--------------------------------------------------------------------------------
1 | package de.slg.ddnss.printertool.clients;
2 |
3 | import static de.slg.ddnss.printertool.clients.AdventurerCommands.CMD_BYE;
4 | import static de.slg.ddnss.printertool.clients.AdventurerCommands.CMD_PRINT_STOP;
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
6 |
7 | import java.io.IOException;
8 | import java.nio.file.Files;
9 | import java.nio.file.Paths;
10 |
11 | import org.junit.jupiter.api.AfterAll;
12 | import org.junit.jupiter.api.BeforeAll;
13 | import org.junit.jupiter.api.Test;
14 |
15 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException;
16 |
17 | class AdventurerClientTest {
18 |
19 | private static String printerAddress;
20 |
21 | @BeforeAll
22 | static void init() {
23 | try {
24 | printerAddress = new FlashForgeDiscoverClient().discoverPrinter().getPrinterAddress();
25 | } catch (Exception e) {
26 | e.printStackTrace();
27 | }
28 | }
29 |
30 | @Test
31 | void printTest() throws FlashForgePrinterException, IOException {
32 | AdventurerClient client = new AdventurerClient(printerAddress);
33 | boolean print = client.print("20mm_Box.gx", Files.readAllBytes(Paths.get("20mm_Box.gx")));
34 | client.close();
35 | assertTrue(print);
36 | }
37 |
38 | @Test
39 | void ledTest() throws FlashForgePrinterException {
40 | AdventurerClient client = new AdventurerClient(printerAddress);
41 | boolean replay = client.setLed(false);
42 | System.out.println(replay);
43 | client.close();
44 | assertTrue(replay);
45 | }
46 |
47 | @Test
48 | void printStopTest() throws FlashForgePrinterException {
49 | AdventurerClient client = new AdventurerClient(printerAddress);
50 | boolean replay = client.stopPrinting();
51 | client.close();
52 | assertTrue(replay);
53 | }
54 |
55 | @Test
56 | void getPrinterInfoTest() throws FlashForgePrinterException {
57 | AdventurerClient client = new AdventurerClient(printerAddress);
58 | PrinterInfo printerInfo = client.getPrinterInfo();
59 | System.out.println(printerInfo);
60 | client.close();
61 | }
62 |
63 | @AfterAll
64 | static void cleanup() throws FlashForgePrinterException {
65 | AdventurerClient adventurerClient = new AdventurerClient(printerAddress);
66 | String sendOk = adventurerClient.sendCommand(CMD_PRINT_STOP);
67 | System.out.println(sendOk);
68 | assertTrue(sendOk.contains("Received"));
69 | assertTrue(sendOk.contains("ok"));
70 |
71 | sendOk = adventurerClient.sendCommand(CMD_BYE);
72 | System.out.println(sendOk);
73 | assertTrue(sendOk.equals("CMD M602 Received.\nControl Release.\nok"));
74 |
75 | adventurerClient.close();
76 | }
77 |
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/src/test/java/de/slg/ddnss/printertool/clients/ByteCRC32.java:
--------------------------------------------------------------------------------
1 | package de.slg.ddnss.printertool.clients;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import org.apache.commons.codec.DecoderException;
6 | import org.apache.commons.codec.binary.Hex;
7 | import org.junit.jupiter.api.Test;
8 |
9 | import de.slg.ddnss.printertool.util.Util;
10 |
11 | class ByteCRC32 {
12 |
13 | String hex = "592d32372e37312046333030300a473120582d31382e363320592d32352e35380a473120582d31332e363520592d32382e30310a473120582d31332e363520592d32382e30310a473120582d31372e383020592d32342e3837204538382e393833362046313830300a473120582d31382e303520592d32342e3336204538382e393931360a473120582d31382e323020592d32342e3537204538382e393935330a473120582d32322e373520592d32312e3132204538392e303736360a473120582d32332e323220592d32312e3737204538392e303838300a473120582d31342e313520592d32382e3634204538392e323530300a473120582d31332e363520592d32382e3031204538392e323631340a473120582d31372e363320592d32342e37332046333030300a473120582d31372e373720592d32342e33390a473120582d31362e323220592d32322e33390a473120582d31312e323020592d32342e38350a473120582d31312e323020592d32342e38350a473120582d31352e353020592d32312e3630204538392e333338322046313830300a473120582d31352e353220592d32312e3338204538392e333431330a473120582d31352e373420592d32312e3332204538392e333434360a473120582d31352e373920592d32312e3338204538392e333435370a473120582d32302e333820592d31372e3931204538392e343237360a473120582d32302e383520592d31382e3535204538392e343338390a473120582d31362e353720592d32312e3739204538392e353135330a473120582d31362e353420592d32322e3032204538392e353138360a473120582d31362e333220592d32322e3038204538392e353231390a473120582d31362e323720592d32322e3032204538392e353233300a473120582d31312e363920592d32352e3438204538392e363034370a473120582d31312e323020592d32342e3835204538392e363136300a473120582d31352e323820592d32312e34382046333030300a473120582d31352e333220592d32312e31340a473120582d31332e383120592d31392e32300a473120582d382e363720592d32312e37350a473120582d382e363720592d32312e37350a473120582d31322e393720592d31382e3439204538392e363932392046313830300a473120582d31332e323220592d31372e3939204538392e373030380a473120582d31332e333720592d31382e3139204538392e373034340a473120582d31382e303720592d31342e3634204538392e373838320a473120582d31382e353220592d31352e3330204538392e373939360a473120582d392e313920592d32322e3336204538392e393636320a473120582d382e363720592d32312e3735204538392e393737360a473120582d31322e383020592d31382e33352046333030300a473120582d31322e393420592d31382e30310a473120582d31312e333920592d31362e30310a473120582d362e303720592d31382e37300a473120582d362e303720592d31382e37300a473120582d31352e383520592d31312e3330204539302e313532312046313830300a473120582d31362e323920592d31312e3937204539302e313633360a473120582d362e363020592d31392e3331204539302e333336360a473120582d362e303720592d31382e3730204539302e333438310a473120582d31302e353920592d31342e39352046333030300a473120582d382e393820592d31322e38320a473120582d332e343620592d31352e36360a473120582d332e343620592d31352e36360a473120582d382e303720592d31322e3137204539302e343330342046313830300a473120582d382e343420592d31312e3637204539302e343339330a473120582d382e353520592d31312e3831204539302e343431380a473120582d31332e363220592d372e3937204539302e353332330a473120582d31342e303820592d382e3633204539302e353433380a473120582d392e353020592d31322e3039204539302e363235350a473120582d392e313420592d31322e3539204539302e363334330a473120582d392e303320592d31322e3435204539302e363336380a473120582d332e393820592d31362e3237204539302e373237300a473120582d332e343620592d31352e3636204539302e373338340a473120582d372e393220592d31322e30322046333030300a473120582d382e313320592d31312e36370a473120582d362e353620592d392e36330a473120582d312e303120592d31322e35300a473120582d312e303120592d31322e35300a473120582d352e373120592d382e3934204539302e383232332046313830300a473120582d352e393920592d382e3433204539302e383330360a473120582d362e313320592d382e3632204539302e383333390a473120582d31312e323620592d342e3734204539302e393235350a473120582d31312e373420592d352e3338204539302e393336390a473120582d372e303420592d382e3934204539312e303230380a473120582d362e373620592d392e3435204539312e303239310a473120582d362e363220592d392e3236204539312e303332350a473120582d312e343920592d31332e3133204539312e313233390a473120582d312e303120592d31322e3530204539312e313335320a473120582d352e353520592d382e38302046333030300a473120582d352e373020592d382e34350a473120582d342e313520592d362e34340a47312058312e343220592d392e33320a47312058312e343220592d392e33320a473120582d332e333020592d352e3735204539312e323139352046313830300a473120582d332e353820592d352e3234204539312e323237370a473120582d332e373220592d352e3433204539312e323331310a473120582d382e383420592d312e3535204539312e333232350a473120582d392e333420592d322e3138204539312e333334300a473120582d342e363320592d352e3735204539312e343138310a473120582d342e333420592d362e3236204539312e343236350a473120582d342e323020592d362e3037204539312e343239380a473120582e393320592d392e3935204539312e353231340a47312058312e343220592d392e3332204539312e353332380a473120582d332e313420592d352e36302046333030300a473120582d332e323920592d352e32360a473120582d312e373420592d332e32350a47312058332e383320592d362e31330a47312058332e383320592d362e31330a473120582d2e383320592d322e3630204539312e363136302046313830300a473120582d312e323020592d322e3130204539312e363234380a473120582d312e333120592d322e3234204539312e363237340a473120582d362e34342059312e3634204539312e373138390a473120582d362e39312059312e3030204539312e373330320a473120582d322e323720592d322e3532204539312e383133310a473120582d312e393020592d332e3032204539312e383232300a473120582d312e373920592d322e3838204539312e383234350a47312058332e333320592d362e3736204539312e393136300a47312058332e383320592d362e3133204539312e393237340a473120582d2e363820592d322e34352046333030300a473120582d2e383920592d322e31300a473120582e363820592d2e30370a47312058362e333120592d322e39390a47312058362e333120592d322e39390a47312058312e353320592e3633204539322e303132382046313830300a47312058312e32352059312e3134204539322e303231310a47312058312e313120592e3935204539322e303234340a473120582d342e31302059342e3839204539322e313137340a473120582d342e35372059342e3234204539322e313238380a473120582e313420592e3638204539322e323132390a473120582e353220592e3137204539322e323231390a473120582e363220592e3331204539322e323234340a47312058352e383120592d332e3632204539322e333137300a47312058362e333120592d322e3939204539322e333238350a47312058312e363920592e37372046333030300a47312058312e35342059312e31320a47312058332e30392059332e31320a47312058382e373320592e32300a47312058382e373320592e32300a47312058342e30342059332e3734204539322e343132312046313830300a47312058332e36312059342e3236204539322e343231370a47312058332e35322059342e3134204539322e343233390a473120582d312e36392059382e3038204539322e353136390a473120582d322e31392059372e3435204539322e353238330a47312058322e35312059332e3930204539322e363132320a47312058322e39362059332e3339204539322e363231380a47312058332e30342059332e3530204539322e363233380a47312058382e323620592d2e3435204539322e373137300a47312058382e373320592e3230204539322e373238340a47312058342e31392059332e39302046333030300a47312058332e39342059342e32370a47312058352e35302059362e33310a4731205831302e39362059332e35320a4731205831302e39362059332e35320a47312058362e33352059372e3032204539322e383130382046313830300a47312058362e30392059372e3533204539322e383138390a47312058352e39332059372e3333204539322e383232360a473120582e3931205931312e3133204539322e393132320a473120582e3337205931302e3534204539322e393233360a47312058342e39372059372e3036204539332e303035370a47312058352e33342059362e3535204539332e303134370a47312058352e34352059362e3639204539332e303137320a4731205831302e35332059322e3835204539332e313037380a4731205831302e39362059332e3532204539332e313139320a47312058362e35322059372e31362046333030300a47312058362e33372059372e35310a47312058372e39322059392e35300a47312058372e39322059392e35300a4731205831332e30382059362e39330a4731205831332e30382059362e39330a47312058332e3631205931342e3130204539332e323838332046313830300a47312058332e3037205931332e3531204539332e323939370a47312058372e3339205931302e3233204539332e333736390a47312058372e37352059392e3733204539332e333835360a47312058372e38362059392e3838204539332e333838330a4731205831322e36362059362e3235204539332e343734300a4731205831332e30382059362e3933204539332e343835330a47312058382e3732205931302e35372046333030300a4731205831302e3333205931322e36390a4731205831352e3234205931302e33320a4731205831352e3234205931302e33320a4731205831312e3136205931332e3431204539332e353538322046313830300a4731205831302e3933205931332e3933204539332e353636330a4731205831302e3736205931332e3731204539332e353730320a47312058362e3238205931372e3039204539332e363530310a473120";
14 |
15 | @Test
16 | void testOddError() throws DecoderException {
17 | System.out.println(hex);
18 | String crc32Checksum = Util.calcChecksum(Hex.decodeHex(hex));
19 | assertEquals("320d9b9", crc32Checksum);
20 | }
21 |
22 | String goldenLast = "5a5aa5a5"
23 | + "0000026d"
24 | + "00000b7e"
25 | + "66d6707b"
26 | + "0a473120582d31352e313820592d32312e38342046333030300a473120582d31352e313820592d32312e38340a473120582d31362e303720592d32322e333020453132332e3231343520463330300a473120582d31362e313620592d32312e383320453132332e323231340a473120582d31362e363020592d32312e393020453132332e323237370a473120582d31362e343020592d32302e393120453132332e323432310a473120582d31352e313820592d32312e383420453132332e323633390a473120582d31352e343120592d32312e33332046333030300a473120582d31332e323020592d31372e39360a3b657874727564655f726174696f3a302e39360a473120582d31332e323020592d31372e39360a473120582d31322e383220592d31372e343520453132332e3237323620463330300a3b70657263656e740a3b657874727564655f726174696f3a310a3b6c617965723a302e30380a3b7368656c6c0a473120453131372e373732362046313830300a473120582d31322e303020592d31382e37322046333030300a4731205a31312e30343020463432300a473120453132332e323732362046313830300a473120582d31322e303020592d31382e37322046333030300a473120582d31322e373220592d31382e313920453132332e3238353320463330300a473120582d31332e393520592d31392e353620453132332e333131350a473120582d31342e343920592d32302e313520453132332e333232390a473120582d31342e383620592d32302e333920453132332e333239320a473120582d31352e303920592d32302e323220453132332e333333330a473120582d31342e393620592d31392e383020453132332e333339350a473120582d31342e353420592d31392e313120453132332e333531300a473120582d31332e353520592d31372e353520453132332e333737330a473120582d31342e323620592d31372e303220453132332e333930300a473120582d31342e373320592d31372e363720453132332e343031340a473120582d31352e323220592d31382e333020453132332e343132370a473120582d31342e343220592d31382e393020453132332e343237300a473120582d31352e383720592d32302e383220453132332e343631320a473120582d31362e313520592d32302e363120453132332e343636320a473120582d31362e323920592d32312e333120453132332e343736340a473120582d31362e313920592d32312e363220453132332e343831300a473120582d31362e303520592d32312e363820453132332e343833320a473120582d31352e393520592d32312e383320453132332e343835370a473120582d31352e353820592d32312e383420453132332e343931300a473120582d31342e393520592d32312e353120453132332e353031310a473120582d31352e323320592d32312e333020453132332e353036310a473120582d31332e373820592d31392e333920453132332e353430330a473120582d31322e393820592d32302e303020453132332e353534360a473120582d31322e303020592d31382e373220453132332e353737350a3b696e66696c6c0a473120582d31322e353420592d31382e36372046333030300a3b657874727564655f726174696f3a312e34360a473120582d31322e353420592d31382e36370a473120582d31332e323020592d31392e343820453132332e3539393220463330300a3b657874727564655f726174696f3a310a473120582d31332e383120592d31392e32302046333030300a473120582d31342e333720592d32302e32390a473120582d31342e383720592d32302e35380a473120582d31352e323720592d32302e32380a473120582d31352e313320592d31392e37320a473120582d31342e323320592d31382e38380a473120582d31342e323320592d31382e38380a473120582d31342e363520592d31382e33380a3b657874727564655f726174696f3a312e34360a473120582d31342e363520592d31382e33380a473120582d31342e303620592d31372e353220453132332e3632303920463330300a3b657874727564655f726174696f3a310a473120582d31342e323320592d31382e38382046333030300a473120582d31352e313320592d31392e37320a473120582d31352e323720592d32302e32380a473120582d31352e313520592d32302e35330a3b657874727564655f726174696f3a302e39360a473120582d31352e313520592d32302e35330a473120582d31352e373520592d32312e333320453132332e3633343620463330300a473120582d31362e303020592d32312e313620453132332e363338370a3b7368656c6c0a3b657874727564655f726174696f3a310a473120453131382e313338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120453132332e363338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120582d31312e383720592d31352e313020453132332e3634383420463330300a3b7368656c6c0a473120453131382e313438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120453132332e363438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120582d31302e383220592d31352e393120453132332e3635373920463330300a3b70657263656e740a3b6c617965723a302e30380a3b7368656c6c0a473120453131382e313537392046313830300a473120582d31322e353020592d31382e33352046333030300a4731205a31312e31323020463432300a473120453132332e363537392046313830300a473120582d31322e353020592d31382e33352046333030300a473120582d31322e363020592d31382e323720453132332e3635393720463330300a473120582d31332e363620592d31392e343820453132332e363832360a473120582d31332e343620592d31392e363320453132332e363836310a473120582d31322e393120592d31392e313020453132332e363937300a473120582d31322e383520592d31392e303320453132332e363938330a473120582d31322e353020592d31382e333520453132332e373039320a3b7368656c6c0a473120453131382e323039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120453132332e373039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120582d31332e373720592d31372e333920453132332e3731313020463330300a473120582d31342e313720592d31372e373920453132332e373139300a473120582d31342e343720592d31382e313620453132332e373235380a473120582d31342e373420592d31382e363620453132332e373333390a473120582d31342e353420592d31382e383220453132332e373337350a473120582d31332e363720592d31372e343620453132332e373630350a3b7368656c6c0a473120453131382e323630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120453132332e373630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120582d31342e363720592d31392e313420453132332e3737373520463330300a3b7368656c6c0a473120453131382e323737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120453132332e373737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120582d31332e393420592d31392e373020453132332e3739343520463330300a4d3130370a3b70657263656e740a473120453131382e323934352046313830300a3b656e642067636f64650a4d3130342053302054300a4d3134302053302054300a47313632205a0a473238205820590a4d31333220582059204120420a4d3635320a4739310a4d31380a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
27 | String last = "0a473120582d31352e313820592d32312e38342046333030300a473120582d31352e313820592d32312e38340a473120582d31362e303720592d32322e333020453132332e3231343520463330300a473120582d31362e313620592d32312e383320453132332e323231340a473120582d31362e363020592d32312e393020453132332e323237370a473120582d31362e343020592d32302e393120453132332e323432310a473120582d31352e313820592d32312e383420453132332e323633390a473120582d31352e343120592d32312e33332046333030300a473120582d31332e323020592d31372e39360a3b657874727564655f726174696f3a302e39360a473120582d31332e323020592d31372e39360a473120582d31322e383220592d31372e343520453132332e3237323620463330300a3b70657263656e740a3b657874727564655f726174696f3a310a3b6c617965723a302e30380a3b7368656c6c0a473120453131372e373732362046313830300a473120582d31322e303020592d31382e37322046333030300a4731205a31312e30343020463432300a473120453132332e323732362046313830300a473120582d31322e303020592d31382e37322046333030300a473120582d31322e373220592d31382e313920453132332e3238353320463330300a473120582d31332e393520592d31392e353620453132332e333131350a473120582d31342e343920592d32302e313520453132332e333232390a473120582d31342e383620592d32302e333920453132332e333239320a473120582d31352e303920592d32302e323220453132332e333333330a473120582d31342e393620592d31392e383020453132332e333339350a473120582d31342e353420592d31392e313120453132332e333531300a473120582d31332e353520592d31372e353520453132332e333737330a473120582d31342e323620592d31372e303220453132332e333930300a473120582d31342e373320592d31372e363720453132332e343031340a473120582d31352e323220592d31382e333020453132332e343132370a473120582d31342e343220592d31382e393020453132332e343237300a473120582d31352e383720592d32302e383220453132332e343631320a473120582d31362e313520592d32302e363120453132332e343636320a473120582d31362e323920592d32312e333120453132332e343736340a473120582d31362e313920592d32312e363220453132332e343831300a473120582d31362e303520592d32312e363820453132332e343833320a473120582d31352e393520592d32312e383320453132332e343835370a473120582d31352e353820592d32312e383420453132332e343931300a473120582d31342e393520592d32312e353120453132332e353031310a473120582d31352e323320592d32312e333020453132332e353036310a473120582d31332e373820592d31392e333920453132332e353430330a473120582d31322e393820592d32302e303020453132332e353534360a473120582d31322e303020592d31382e373220453132332e353737350a3b696e66696c6c0a473120582d31322e353420592d31382e36372046333030300a3b657874727564655f726174696f3a312e34360a473120582d31322e353420592d31382e36370a473120582d31332e323020592d31392e343820453132332e3539393220463330300a3b657874727564655f726174696f3a310a473120582d31332e383120592d31392e32302046333030300a473120582d31342e333720592d32302e32390a473120582d31342e383720592d32302e35380a473120582d31352e323720592d32302e32380a473120582d31352e313320592d31392e37320a473120582d31342e323320592d31382e38380a473120582d31342e323320592d31382e38380a473120582d31342e363520592d31382e33380a3b657874727564655f726174696f3a312e34360a473120582d31342e363520592d31382e33380a473120582d31342e303620592d31372e353220453132332e3632303920463330300a3b657874727564655f726174696f3a310a473120582d31342e323320592d31382e38382046333030300a473120582d31352e313320592d31392e37320a473120582d31352e323720592d32302e32380a473120582d31352e313520592d32302e35330a3b657874727564655f726174696f3a302e39360a473120582d31352e313520592d32302e35330a473120582d31352e373520592d32312e333320453132332e3633343620463330300a473120582d31362e303020592d32312e313620453132332e363338370a3b7368656c6c0a3b657874727564655f726174696f3a310a473120453131382e313338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120453132332e363338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120582d31312e383720592d31352e313020453132332e3634383420463330300a3b7368656c6c0a473120453131382e313438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120453132332e363438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120582d31302e383220592d31352e393120453132332e3635373920463330300a3b70657263656e740a3b6c617965723a302e30380a3b7368656c6c0a473120453131382e313537392046313830300a473120582d31322e353020592d31382e33352046333030300a4731205a31312e31323020463432300a473120453132332e363537392046313830300a473120582d31322e353020592d31382e33352046333030300a473120582d31322e363020592d31382e323720453132332e3635393720463330300a473120582d31332e363620592d31392e343820453132332e363832360a473120582d31332e343620592d31392e363320453132332e363836310a473120582d31322e393120592d31392e313020453132332e363937300a473120582d31322e383520592d31392e303320453132332e363938330a473120582d31322e353020592d31382e333520453132332e373039320a3b7368656c6c0a473120453131382e323039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120453132332e373039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120582d31332e373720592d31372e333920453132332e3731313020463330300a473120582d31342e313720592d31372e373920453132332e373139300a473120582d31342e343720592d31382e313620453132332e373235380a473120582d31342e373420592d31382e363620453132332e373333390a473120582d31342e353420592d31382e383220453132332e373337350a473120582d31332e363720592d31372e343620453132332e373630350a3b7368656c6c0a473120453131382e323630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120453132332e373630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120582d31342e363720592d31392e313420453132332e3737373520463330300a3b7368656c6c0a473120453131382e323737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120453132332e373737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120582d31332e393420592d31392e373020453132332e3739343520463330300a4d3130370a3b70657263656e740a473120453131382e323934352046313830300a3b656e642067636f64650a4d3130342053302054300a4d3134302053302054300a47313632205a0a473238205820590a4d31333220582059204120420a4d3635320a4739310a4d31380a";
28 | @Test
29 | void testLastPackage() throws DecoderException {
30 | String crc32Checksum = Util.calcChecksum(Hex.decodeHex(last));
31 | assertEquals("66d6707b", crc32Checksum);
32 | }
33 |
34 | String box_gold = "5a5aa5a50000004a00000"
35 | + "828"
36 | + "b5f2a7b23120582d382e32362059332e303120453838342e313431330a473120582d382e32362059332e353820453838342e313537360a47312058332e353820592d382e323620453838342e363334330a47312058342e313520592d382e323620453838342e363530360a473120582d382e32362059342e313520453838352e313530330a473120582d382e32362059342e373220453838352e313636350a47312058342e373220592d382e323620453838352e363839310a47312058352e333020592d382e323620453838352e373035360a473120582d382e32362059352e333020453838362e323531360a473120582d382e32362059352e383720453838362e323637390a47312058352e383720592d382e323620453838362e383336380a47312058362e343420592d382e323620453838362e383533310a473120582d382e32362059362e343420453838372e343434390a473120582d382e32362059372e303220453838372e343631340a47312058372e303220592d382e323620453838382e303736370a47312058372e353920592d382e323620453838382e303933300a473120582d382e32362059372e353920453838382e373331310a473120582d382e32362059382e313620453838382e373437340a47312058382e313620592d382e323620453838392e343038350a47312058382e323620592d382e323620453838392e343131340a47312058382e323620592d372e373820453838392e343235300a473120582d372e37382059382e323620453839302e303730390a473120582d372e32312059382e323620453839302e303837310a47312058382e323620592d372e323120453839302e373130300a47312058382e323620592d362e363420453839302e373236330a473120582d362e36342059382e323620453839312e333236320a473120582d362e30372059382e323620453839312e333432340a47312058382e323620592d362e303720453839312e393139340a47312058382e323620592d352e343920453839312e393335390a473120582d352e34392059382e323620453839322e343839360a473120582d342e39322059382e323620453839322e353035380a47312058382e323620592d342e393220453839332e303336350a47312058382e323620592d342e333520453839332e303532370a473120582d342e33352059382e323620453839332e353630350a473120582d332e37372059382e323620453839332e353737300a47312058382e323620592d332e373720453839342e303631340a47312058382e323620592d332e323020453839342e303737360a473120582d332e32302059382e323620453839342e353339310a473120582d322e36332059382e323620453839342e353535330a47312058382e323620592d322e363320453839342e393933380a47312058382e323620592d322e303620453839352e303130310a473120582d322e30362059382e323620453839352e343235350a473120582d312e34382059382e323620453839352e343432310a47312058382e323620592d312e343820453839352e383334320a47312058382e323620592d2e393120453839352e383530350a473120582d2e39312059382e323620453839362e323139370a473120582d2e33342059382e323620453839362e323336300a47312058382e323620592d2e333420453839362e353832320a47312058382e323620592e323320453839362e353938340a473120582e32332059382e323620453839362e393231380a473120582e38312059382e323620453839362e393338320a47312058382e323620592e383120453839372e323338320a47312058382e32362059312e333820453839372e323534350a47312058312e33382059382e323620453839372e353331340a47312058312e39352059382e323620453839372e353437370a47312058382e32362059312e393520453839";
37 | String box_last = "5a5aa5a50000004a00000"
38 | + "b7e"
39 | + "b5f2a7b23120582d382e32362059332e303120453838342e313431330a473120582d382e32362059332e353820453838342e313537360a47312058332e353820592d382e323620453838342e363334330a47312058342e313520592d382e323620453838342e363530360a473120582d382e32362059342e313520453838352e313530330a473120582d382e32362059342e373220453838352e313636350a47312058342e373220592d382e323620453838352e363839310a47312058352e333020592d382e323620453838352e373035360a473120582d382e32362059352e333020453838362e323531360a473120582d382e32362059352e383720453838362e323637390a47312058352e383720592d382e323620453838362e383336380a47312058362e343420592d382e323620453838362e383533310a473120582d382e32362059362e343420453838372e343434390a473120582d382e32362059372e303220453838372e343631340a47312058372e303220592d382e323620453838382e303736370a47312058372e353920592d382e323620453838382e303933300a473120582d382e32362059372e353920453838382e373331310a473120582d382e32362059382e313620453838382e373437340a47312058382e313620592d382e323620453838392e343038350a47312058382e323620592d382e323620453838392e343131340a47312058382e323620592d372e373820453838392e343235300a473120582d372e37382059382e323620453839302e303730390a473120582d372e32312059382e323620453839302e303837310a47312058382e323620592d372e323120453839302e373130300a47312058382e323620592d362e363420453839302e373236330a473120582d362e36342059382e323620453839312e333236320a473120582d362e30372059382e323620453839312e333432340a47312058382e323620592d362e303720453839312e393139340a47312058382e323620592d352e343920453839312e393335390a473120582d352e34392059382e323620453839322e343839360a473120582d342e39322059382e323620453839322e353035380a47312058382e323620592d342e393220453839332e303336350a47312058382e323620592d342e333520453839332e303532370a473120582d342e33352059382e323620453839332e353630350a473120582d332e37372059382e323620453839332e353737300a47312058382e323620592d332e373720453839342e303631340a47312058382e323620592d332e323020453839342e303737360a473120582d332e32302059382e323620453839342e353339310a473120582d322e36332059382e323620453839342e353535330a47312058382e323620592d322e363320453839342e393933380a47312058382e323620592d322e303620453839352e303130310a473120582d322e30362059382e323620453839352e343235350a473120582d312e34382059382e323620453839352e343432310a47312058382e323620592d312e343820453839352e383334320a47312058382e323620592d2e393120453839352e383530350a473120582d2e39312059382e323620453839362e323139370a473120582d2e33342059382e323620453839362e323336300a47312058382e323620592d2e333420453839362e353832320a47312058382e323620592e323320453839362e353938340a473120582e32332059382e323620453839362e393231380a473120582e38312059382e323620453839362e393338320a47312058382e323620592e383120453839372e323338320a47312058382e32362059312e333820453839372e323534350a47312058312e33382059382e323620453839372e353331340a47312058312e39352059382e323620453839372e353437370a47312058382e32362059312e393520453839";
40 | @Test
41 | void testBoxLastPackage() throws DecoderException {
42 | String crc32Checksum = Util.calcChecksum(Hex.decodeHex(box_last));
43 | assertEquals("4d7ad8c9", crc32Checksum);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/de/slg/ddnss/printertool/clients/FlashForgeDiscoverClientTest.java:
--------------------------------------------------------------------------------
1 | package de.slg.ddnss.printertool.clients;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertNotNull;
4 |
5 | import org.junit.jupiter.api.Test;
6 | import org.junit.jupiter.api.Timeout;
7 |
8 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException;
9 |
10 | class FlashForgeDiscoverClientTest {
11 |
12 | @Test
13 | @Timeout(value = 5)
14 | void discoverTest() throws FlashForgePrinterException {
15 | FlashForgeDiscoverClient client = new FlashForgeDiscoverClient();
16 | assertNotNull(client.discoverPrinter().getPrinterAddress());
17 | assertNotNull(client.getPrinterAddress());
18 | assertNotNull(client.getPrinterName());
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/test/java/de/slg/ddnss/printertool/clients/JUnit5TestSuite.java:
--------------------------------------------------------------------------------
1 | package de.slg.ddnss.printertool.clients;
2 |
3 | import org.junit.platform.runner.JUnitPlatform;
4 | import org.junit.platform.suite.api.SelectPackages;
5 | import org.junit.runner.RunWith;
6 |
7 | @RunWith(JUnitPlatform.class)
8 | @SelectPackages("de.slg.ddnss.printertool.clients")
9 | public class JUnit5TestSuite {
10 | }
--------------------------------------------------------------------------------
/src/test/java/de/slg/ddnss/printertool/clients/TcpClientTest.java:
--------------------------------------------------------------------------------
1 | package de.slg.ddnss.printertool.clients;
2 |
3 | import static de.slg.ddnss.printertool.clients.AdventurerCommands.*;
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
7 |
8 | import java.io.IOException;
9 | import java.nio.file.Files;
10 | import java.nio.file.Path;
11 | import java.nio.file.Paths;
12 | import java.util.List;
13 |
14 | import org.junit.jupiter.api.AfterAll;
15 | import org.junit.jupiter.api.Assertions;
16 | import org.junit.jupiter.api.BeforeAll;
17 | import org.junit.jupiter.api.Test;
18 |
19 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException;
20 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterTransferException;
21 | import de.slg.ddnss.printertool.util.Util;
22 |
23 | class TcpClientTest {
24 |
25 | private static TcpPrinterClient client;
26 | private static String printerAddress;
27 |
28 | @BeforeAll
29 | static void init() {
30 | try {
31 | FlashForgeDiscoverClient flashForgeDiscoverClient = new FlashForgeDiscoverClient();
32 | flashForgeDiscoverClient.discoverPrinter();
33 | printerAddress = flashForgeDiscoverClient.getPrinterAddress();
34 | assertNotNull(printerAddress);
35 | client = new TcpPrinterClient(printerAddress);
36 | assertNotNull(client);
37 | } catch (FlashForgePrinterException e) {
38 | e.printStackTrace();
39 | }
40 | }
41 |
42 | @Test
43 | void m601test() throws FlashForgePrinterException {
44 | String sendOk = client.sendCommand(CMD_HELLO);
45 | System.out.println(sendOk);
46 | assertEquals("CMD M601 Received.\nControl Success.\nok", sendOk);
47 | }
48 |
49 | @Test
50 | void m115test() throws FlashForgePrinterException {
51 | String sendOk = client.sendCommand(CMD_INFO_STATUS);
52 | System.out.println(sendOk);
53 | assertEquals("CMD M115 Received.\nMachine Type: FlashForge Adventurer III\n" + "Machine Name: My 3D Printer\n"
54 | + "Firmware: v1.1.7\n" + "SN: SNFFAD229083\n" + "X: 150 Y: 150 Z: 150\n" + "Tool Count: 1\n"
55 | + "Mac Address: 88:A9:A7:90:77:A5\n\nok", sendOk);
56 | }
57 |
58 | @Test
59 | void m650test() throws FlashForgePrinterException {
60 | String sendOk = client.sendCommand(CMD_INFO_CAL);
61 | System.out.println(sendOk);
62 | assertEquals("CMD M650 Received.\nX: 1.0 Y: 0.5\nok", sendOk);
63 | }
64 |
65 | @Test
66 | void m114test() throws FlashForgePrinterException {
67 | String sendOk = client.sendCommand(CMD_INFO_XYZAB);
68 | System.out.println(sendOk);
69 | }
70 |
71 | @Test
72 | void m119test() throws FlashForgePrinterException {
73 | String sendOk = client.sendCommand(CMD_INFO);
74 | System.out.println(sendOk);
75 | assertTrue(sendOk.contains("Received"));
76 | assertTrue(sendOk.contains("ok"));
77 | }
78 |
79 | @Test
80 | void m27test() throws FlashForgePrinterException {
81 | String sendOk;
82 | sendOk = client.sendCommand(CMD_PRINT_STATUS);
83 | System.out.println(sendOk);
84 | assertTrue(sendOk.contains("Received"));
85 | assertTrue(sendOk.contains("ok"));
86 | }
87 |
88 | @Test
89 | void m105test() throws FlashForgePrinterException {
90 | String sendOk;
91 | sendOk = client.sendCommand(CMD_TEMP);
92 | System.out.println(sendOk);
93 | assertTrue(sendOk.contains("Received"));
94 | assertTrue(sendOk.contains("ok"));
95 | }
96 |
97 | @Test
98 | void ledTest() throws FlashForgePrinterException {
99 | String sendOk;
100 | sendOk = client.sendCommand(CMD_LED_OFF);
101 | System.out.println(sendOk);
102 | assertTrue(sendOk.contains("Received"));
103 | assertTrue(sendOk.contains("ok"));
104 |
105 | sendOk = client.sendCommand(CMD_LED_ON);
106 | System.out.println(sendOk);
107 | assertTrue(sendOk.contains("Received"));
108 | assertTrue(sendOk.contains("ok"));
109 | }
110 |
111 | @Test
112 | void printTest() throws FlashForgePrinterException {
113 | try {
114 | Path fileToPrint = Paths.get("20mm_Box.gx");
115 | byte[] readAllLines = Files.readAllBytes(fileToPrint);
116 | String filename = fileToPrint.getFileName().toString();
117 | System.out.println("File: " + filename + "/" + readAllLines.length + "byte");
118 |
119 | System.out.println(client.sendCommand(CMD_PREPARE_PRINT.replaceAll("%%size%%", "" + readAllLines.length)
120 | .replaceAll("%%filename%%", filename)));
121 |
122 | try {
123 | List gcode = Util.prepareRawData(readAllLines);
124 | client.sendRawData(gcode);
125 | System.out.println(client.sendCommand(CMD_SAVE_FILE));
126 | System.out.println(client.sendCommand(CMD_PRINT_START.replaceAll("%%filename%%", filename)));
127 |
128 | System.out.println(client.sendCommand(CMD_PRINT_STATUS));
129 | } catch (FlashForgePrinterTransferException e) {
130 | e.printStackTrace();
131 | }
132 |
133 | } catch (IOException e) {
134 | e.printStackTrace();
135 | }
136 | }
137 |
138 | @Test
139 | void unknownHostExceptionTest() {
140 | Assertions.assertThrows(FlashForgePrinterTransferException.class, () -> {
141 | new TcpPrinterClient("0.0.0.a");
142 | });
143 | }
144 |
145 | @AfterAll
146 | static void printStopTest() throws FlashForgePrinterException {
147 | String sendOk = client.sendCommand(CMD_PRINT_STOP);
148 | System.out.println(sendOk);
149 | assertTrue(sendOk.contains("Received"));
150 | assertTrue(sendOk.contains("ok"));
151 |
152 | sendOk = client.sendCommand(CMD_BYE);
153 | System.out.println(sendOk);
154 | assertTrue(sendOk.equals("CMD M602 Received.\nControl Release.\nok"));
155 | client.close();
156 | }
157 |
158 | }
159 |
--------------------------------------------------------------------------------
/src/test/java/de/slg/ddnss/printertool/clients/UpdClientTest.java:
--------------------------------------------------------------------------------
1 | package de.slg.ddnss.printertool.clients;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertNotNull;
4 |
5 | import java.net.DatagramPacket;
6 |
7 | import org.junit.jupiter.api.BeforeAll;
8 | import org.junit.jupiter.api.Test;
9 | import org.junit.jupiter.api.Timeout;
10 |
11 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException;
12 |
13 | class UpdClientTest {
14 |
15 | static UdpDiscoveryClient client;
16 |
17 | @BeforeAll
18 | static void init() throws FlashForgePrinterException {
19 | client = new UdpDiscoveryClient();
20 | }
21 |
22 | @Test
23 | @Timeout(value = 5)
24 | void test() throws FlashForgePrinterException {
25 | DatagramPacket receiveMessage = client.sendMessage("c0a800de46500000");
26 | assertNotNull(receiveMessage);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/resources/20mm_Box.gx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Slugger2k/FlashForgePrinterApi/6a30121df522d5054838f0c0f876fd4ffcfb8327/src/test/resources/20mm_Box.gx
--------------------------------------------------------------------------------
/src/test/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | %d{dd.MM.yyyy HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{50}.%M - %msg%n
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------