Skip to content

Commit d05a658

Browse files
committed
Add missing cast and null check. Some more javadoc
1 parent 1892282 commit d05a658

File tree

8 files changed

+215
-52
lines changed

8 files changed

+215
-52
lines changed

java/src/main/java/io/github/flanglet/kanzi/Error.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,104 @@
2121
*/
2222
public final class Error {
2323

24+
/**
25+
* Missing paraneter
26+
*/
2427
public static final int ERR_MISSING_PARAM = 1;
28+
29+
/**
30+
* Invalid block size
31+
*/
2532
public static final int ERR_BLOCK_SIZE = 2;
33+
34+
/**
35+
* Invalid entropy coded
36+
*/
2637
public static final int ERR_INVALID_CODEC = 3;
38+
39+
/**
40+
* Failure to create a compressor
41+
*/
2742
public static final int ERR_CREATE_COMPRESSOR = 4;
43+
44+
/**
45+
* Failure to create a decompressor
46+
*/
2847
public static final int ERR_CREATE_DECOMPRESSOR = 5;
48+
49+
/**
50+
* The output should is a folder
51+
*/
2952
public static final int ERR_OUTPUT_IS_DIR = 6;
53+
54+
/**
55+
* Failure to ovwerwrite a file
56+
*/
3057
public static final int ERR_OVERWRITE_FILE = 7;
58+
59+
/**
60+
* Failure to create a file
61+
*/
3162
public static final int ERR_CREATE_FILE = 8;
63+
64+
/**
65+
* Failure to create a bit stream
66+
*/
3267
public static final int ERR_CREATE_BITSTREAM = 9;
68+
69+
/**
70+
* Failure to open a file
71+
*/
3372
public static final int ERR_OPEN_FILE = 10;
73+
74+
/**
75+
* Failure to read a file
76+
*/
3477
public static final int ERR_READ_FILE = 11;
78+
79+
/**
80+
* Failure to write a file
81+
*/
3582
public static final int ERR_WRITE_FILE = 12;
83+
84+
/**
85+
* Failure to process a block of data
86+
*/
3687
public static final int ERR_PROCESS_BLOCK = 13;
88+
89+
/**
90+
* Failure to create an entropy coded
91+
*/
3792
public static final int ERR_CREATE_CODEC = 14;
93+
94+
/**
95+
* Invalid file
96+
*/
3897
public static final int ERR_INVALID_FILE = 15;
98+
99+
/**
100+
* Invalid or unsupported bit stream version
101+
*/
39102
public static final int ERR_STREAM_VERSION = 16;
103+
104+
/**
105+
* Failure to create a stream
106+
*/
40107
public static final int ERR_CREATE_STREAM = 17;
108+
109+
/**
110+
* Invalid parameter
111+
*/
41112
public static final int ERR_INVALID_PARAM = 18;
113+
114+
/**
115+
* Checksum failure
116+
*/
42117
public static final int ERR_CRC_CHECK = 19;
118+
119+
/**
120+
* Unknown error
121+
*/
43122
public static final int ERR_UNKNOWN = 127;
44123

45124
/**

java/src/main/java/io/github/flanglet/kanzi/Event.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,74 @@ public class Event {
2626
* Enum representing the types of events that can occur.
2727
*/
2828
public enum Type {
29+
/**
30+
* Beginning of compression
31+
*/
2932
COMPRESSION_START,
33+
34+
/**
35+
* Beginning of decompression
36+
*/
3037
DECOMPRESSION_START,
38+
39+
/**
40+
* Beginning of transform
41+
*/
3142
BEFORE_TRANSFORM,
43+
44+
/**
45+
* End of transform
46+
*/
3247
AFTER_TRANSFORM,
48+
49+
/**
50+
* Beginning of entropy
51+
*/
3352
BEFORE_ENTROPY,
53+
54+
/**
55+
* End of entropy
56+
*/
3457
AFTER_ENTROPY,
58+
59+
/**
60+
* End of compression
61+
*/
3562
COMPRESSION_END,
63+
64+
/**
65+
* End of dcompression
66+
*/
3667
DECOMPRESSION_END,
68+
69+
/**
70+
* End of header decoding
71+
*/
3772
AFTER_HEADER_DECODING,
73+
74+
/**
75+
* Block informartion
76+
*/
3877
BLOCK_INFO
3978
}
4079

4180
/**
4281
* Enum representing the types of hash used in the events.
4382
*/
4483
public enum HashType {
84+
/**
85+
* No hash
86+
*/
4587
NO_HASH,
88+
89+
/**
90+
* 32 bit hash
91+
*/
92+
4693
SIZE_32,
94+
/**
95+
* 64 bit hash
96+
*/
4797
SIZE_64
4898
}
4999

java/src/main/java/io/github/flanglet/kanzi/app/BlockCompressor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ else if ((inputIsDir == true) && (specialOutput == false))
444444
this.blockSize = (int) Math.max(Math.min(bl, MAX_BLOCK_SIZE), MIN_BLOCK_SIZE);
445445
}
446446

447-
if (oName.isEmpty()
447+
if (oName.isEmpty())
448448
{
449449
oName = iName + ".bak";
450450
}

java/src/main/java/io/github/flanglet/kanzi/app/BlockDecompressor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ else if ((inputIsDir == true) && (specialOutput == false))
338338
long fileSize = Files.size(file);
339339
Map<String, Object> taskCtx = new HashMap<>(ctx);
340340

341-
if (oName.isEmpty()
341+
if (oName.isEmpty())
342342
{
343343
oName = iName + ".bak";
344344
}

java/src/main/java/io/github/flanglet/kanzi/bitstream/DefaultOutputBitStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private void flush() throws BitStreamException {
237237
try {
238238
if (this.position > 0) {
239239
this.os.write(this.buffer, 0, this.position);
240-
this.written += (this.position << 3);
240+
this.written += (((long) this.position) << 3);
241241
this.position = 0;
242242
}
243243
} catch (IOException e) {

java/src/main/java/io/github/flanglet/kanzi/transform/BWT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ public BWT()
9898
// Number of jobs provided in the context
9999
public BWT(Map<String, Object> ctx)
100100
{
101-
final int tasks = (Integer) ctx.get("jobs");
101+
final int tasks = (ctx == null) ? 1 : (Integer) ctx.get("jobs");
102102

103103
if (tasks <= 0)
104-
throw new IllegalArgumentException("The number of jobs must be in positive");
104+
throw new IllegalArgumentException("The number of jobs must be positive");
105105

106-
ExecutorService threadPool = (ExecutorService) ctx.get("pool");
106+
ExecutorService threadPool = (ctx == null) ? null : (ExecutorService) ctx.get("pool");
107107

108108
if ((tasks > 1) && (threadPool == null))
109109
throw new IllegalArgumentException("The thread pool cannot be null when the number of jobs is "+tasks);

java/src/main/java/io/github/flanglet/kanzi/transform/DivSufSort.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,9 @@ else if (limit == -2)
20412041
// tandem repeat copy
20422042
StackElement se = this.trStack.pop();
20432043

2044+
if (se == null)
2045+
return;
2046+
20442047
if (se.d == 0)
20452048
{
20462049
this.trCopy(isa, first, se.b, se.c, last, isad - isa);
Lines changed: 77 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,77 @@
1-
/*
2-
Copyright 2011-2024 Frederic Langlet
3-
Licensed under the Apache License, Version 2.0 (the "License");
4-
you may not use this file except in compliance with the License.
5-
you may obtain a copy of the License at
6-
7-
http://www.apache.org/licenses/LICENSE-2.0
8-
9-
Unless required by applicable law or agreed to in writing, software
10-
distributed under the License is distributed on an "AS IS" BASIS,
11-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
See the License for the specific language governing permissions and
13-
limitations under the License.
14-
*/
15-
16-
package io.github.flanglet.kanzi.util.sort;
17-
18-
import io.github.flanglet.kanzi.ArrayComparator;
19-
20-
21-
public final class DefaultArrayComparator implements ArrayComparator
22-
{
23-
private final int[] array;
24-
25-
26-
public DefaultArrayComparator(int[] array)
27-
{
28-
if (array == null)
29-
throw new NullPointerException("Invalid null array parameter");
30-
31-
this.array = array;
32-
}
33-
34-
35-
@Override
36-
public int compare(int lidx, int ridx)
37-
{
38-
int res = this.array[lidx] - this.array[ridx];
39-
40-
// Make the sort stable
41-
if (res == 0)
42-
res = lidx - ridx;
43-
44-
return res;
45-
}
46-
}
1+
/*
2+
Copyright 2011-2024 Frederic Langlet
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
you may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package io.github.flanglet.kanzi.util.sort;
17+
18+
import io.github.flanglet.kanzi.ArrayComparator;
19+
20+
/**
21+
* A comparator for comparing elements in an integer array. This class implements the {@link ArrayComparator} interface
22+
* and provides a mechanism to compare two elements based on their values. The comparison also accounts for stable sorting
23+
* by considering their indices when the values are equal.
24+
*
25+
* <p>This class is immutable and thread-safe as it holds a reference to the input array but does not modify it.</p>
26+
*
27+
* <p>Example usage:</p>
28+
* <pre>
29+
* int[] array = { 5, 2, 8, 1 };
30+
* DefaultArrayComparator comparator = new DefaultArrayComparator(array);
31+
* int result = comparator.compare(0, 1); // Compares array[0] (5) and array[1] (2)
32+
* </pre>
33+
*
34+
* @see ArrayComparator
35+
*/
36+
public final class DefaultArrayComparator implements ArrayComparator {
37+
38+
private final int[] array;
39+
40+
/**
41+
* Constructs a new {@code DefaultArrayComparator} using the specified integer array.
42+
*
43+
* @param array the array to compare elements in; must not be {@code null}
44+
* @throws NullPointerException if the provided array is {@code null}
45+
*/
46+
public DefaultArrayComparator(int[] array) {
47+
if (array == null)
48+
throw new NullPointerException("Invalid null array parameter");
49+
50+
this.array = array;
51+
}
52+
53+
/**
54+
* Compares two elements of the array at the specified indices.
55+
* <p>
56+
* The comparison is based on the values of the elements at the provided indices. If the values are equal,
57+
* the method returns a comparison based on their indices to maintain stability in sorting.
58+
* </p>
59+
*
60+
* @param lidx the index of the first element to compare
61+
* @param ridx the index of the second element to compare
62+
* @return a negative integer if the element at {@code lidx} is less than the element at {@code ridx},
63+
* a positive integer if the element at {@code lidx} is greater than the element at {@code ridx},
64+
* or zero if they are equal
65+
*/
66+
@Override
67+
public int compare(int lidx, int ridx) {
68+
int res = this.array[lidx] - this.array[ridx];
69+
70+
// Make the sort stable
71+
if (res == 0)
72+
res = lidx - ridx;
73+
74+
return res;
75+
}
76+
}
77+

0 commit comments

Comments
 (0)