Skip to content

Commit 74e7d5f

Browse files
committed
Merge branch 'develop'
2 parents fd580ac + ae6167e commit 74e7d5f

File tree

22 files changed

+188
-44
lines changed

22 files changed

+188
-44
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
## [0.0.25] - 2023-07-18
11+
12+
### Fixed
13+
14+
- Fix OverlappingFileLockException
15+
1016
## [0.0.24] - 2023-06-30
1117

1218
### Added
@@ -224,7 +230,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
224230

225231
- Initial release
226232

227-
[Unreleased]: https://github.com/nbbrd/java-io-util/compare/v0.0.24...HEAD
233+
[Unreleased]: https://github.com/nbbrd/java-io-util/compare/v0.0.25...HEAD
234+
[0.0.25]: https://github.com/nbbrd/java-io-util/compare/v0.0.24...v0.0.25
228235
[0.0.24]: https://github.com/nbbrd/java-io-util/compare/v0.0.23...v0.0.24
229236
[0.0.23]: https://github.com/nbbrd/java-io-util/compare/v0.0.22...v0.0.23
230237
[0.0.22]: https://github.com/nbbrd/java-io-util/compare/v0.0.21...v0.0.22

java-io-base/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.github.nbbrd.java-io-util</groupId>
77
<artifactId>java-io-parent</artifactId>
8-
<version>0.0.24</version>
8+
<version>0.0.25</version>
99
</parent>
1010

1111
<artifactId>java-io-base</artifactId>

java-io-base/src/main/java/internal/io/DecodingFileFormatter.java renamed to java-io-base/src/main/java/internal/io/DecodingFileParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import static nbbrd.io.Resource.uncloseableInputStream;
1414

1515
@lombok.RequiredArgsConstructor
16-
public final class DecodingFileFormatter<T> implements FileParser<T> {
16+
public final class DecodingFileParser<T> implements FileParser<T> {
1717

1818
@NonNull
1919
final FileParser<T> parser;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package internal.io;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
8+
@lombok.experimental.UtilityClass
9+
public class InternalResource {
10+
11+
public static final int DEFAULT_BUFFER_SIZE = 8192;
12+
13+
public static byte[] readAllBytes(InputStream stream) throws IOException {
14+
ByteArrayOutputStream result = new ByteArrayOutputStream();
15+
transferTo(stream, result);
16+
return result.toByteArray();
17+
}
18+
19+
public static void transferTo(InputStream input, OutputStream output) throws IOException {
20+
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
21+
int read;
22+
while ((read = input.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
23+
output.write(buffer, 0, read);
24+
}
25+
}
26+
}

java-io-base/src/main/java/internal/io/LockingFileFormatter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.NonNull;
44
import nbbrd.io.FileFormatter;
5+
import nbbrd.io.WrappedIOException;
56
import nbbrd.io.function.IOSupplier;
67

78
import java.io.File;
@@ -11,6 +12,7 @@
1112
import java.nio.channels.Channels;
1213
import java.nio.channels.FileChannel;
1314
import java.nio.channels.FileLock;
15+
import java.nio.channels.OverlappingFileLockException;
1416
import java.nio.file.Path;
1517
import java.nio.file.StandardOpenOption;
1618

@@ -26,6 +28,8 @@ public void formatFile(@NonNull T value, @NonNull File target) throws IOExceptio
2628
try (FileOutputStream stream = new FileOutputStream(checkTarget(target))) {
2729
try (FileLock ignore = stream.getChannel().lock()) {
2830
delegate.formatStream(value, stream);
31+
} catch (OverlappingFileLockException ex) {
32+
throw WrappedIOException.wrap(ex);
2933
}
3034
}
3135
}
@@ -35,6 +39,8 @@ public void formatPath(@NonNull T value, @NonNull Path target) throws IOExceptio
3539
try (FileChannel channel = FileChannel.open(checkTarget(target), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
3640
try (FileLock ignore = channel.lock()) {
3741
delegate.formatStream(value, Channels.newOutputStream(channel));
42+
} catch (OverlappingFileLockException ex) {
43+
throw WrappedIOException.wrap(ex);
3844
}
3945
}
4046
}

java-io-base/src/main/java/internal/io/LockingFileParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.NonNull;
44
import nbbrd.io.FileParser;
5+
import nbbrd.io.WrappedIOException;
56
import nbbrd.io.function.IOSupplier;
67

78
import java.io.File;
@@ -11,6 +12,7 @@
1112
import java.nio.channels.Channels;
1213
import java.nio.channels.FileChannel;
1314
import java.nio.channels.FileLock;
15+
import java.nio.channels.OverlappingFileLockException;
1416
import java.nio.file.Path;
1517
import java.nio.file.StandardOpenOption;
1618

@@ -26,6 +28,8 @@ public final class LockingFileParser<T> implements FileParser<T> {
2628
try (FileInputStream stream = new FileInputStream(checkSource(source))) {
2729
try (FileLock ignore = stream.getChannel().lock(0, Long.MAX_VALUE, true)) {
2830
return delegate.parseStream(stream);
31+
} catch (OverlappingFileLockException ex) {
32+
throw WrappedIOException.wrap(ex);
2933
}
3034
}
3135
}
@@ -35,6 +39,8 @@ public final class LockingFileParser<T> implements FileParser<T> {
3539
try (FileChannel channel = FileChannel.open(checkSource(source), StandardOpenOption.READ)) {
3640
try (FileLock ignore = channel.lock(0, Long.MAX_VALUE, true)) {
3741
return delegate.parseStream(Channels.newInputStream(channel));
42+
} catch (OverlappingFileLockException ex) {
43+
throw WrappedIOException.wrap(ex);
3844
}
3945
}
4046
}

java-io-base/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
exports nbbrd.io.text;
2929
exports nbbrd.io.zip;
3030

31+
exports internal.io to nbbrd.io.xml, nbbrd.io.xml.bind, nbbrd.io.picocsv;
3132
exports internal.io.text to nbbrd.io.xml, nbbrd.io.xml.bind, nbbrd.io.picocsv;
3233
}

java-io-base/src/main/java/nbbrd/io/FileParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package nbbrd.io;
22

33
import internal.io.AndThenFileParser;
4-
import internal.io.DecodingFileFormatter;
4+
import internal.io.DecodingFileParser;
55
import internal.io.FunctionalFileParser;
66
import internal.io.LockingFileParser;
77
import internal.io.text.LegacyFiles;
@@ -58,7 +58,7 @@ public interface FileParser<T> {
5858

5959
@StaticFactoryMethod
6060
static <T> @NonNull FileParser<T> onParsingGzip(@NonNull FileParser<T> parser) {
61-
return new DecodingFileFormatter<>(parser, GZIPInputStream::new);
61+
return new DecodingFileParser<>(parser, GZIPInputStream::new);
6262
}
6363

6464
@StaticFactoryMethod

java-io-base/src/main/java/nbbrd/io/zip/Zip.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package nbbrd.io.zip;
1818

19+
import internal.io.InternalResource;
1920
import lombok.NonNull;
2021
import nbbrd.io.Resource;
2122
import nbbrd.io.function.IOPredicate;
@@ -99,12 +100,8 @@ private byte[] toByteArray(ZipEntry entry, ZipInputStream stream) throws IOExcep
99100
if (size >= Integer.MAX_VALUE) {
100101
throw new IOException("ZIP entry size is too large");
101102
}
102-
ByteArrayOutputStream result = new ByteArrayOutputStream(size > 0 ? (int) size : 4096);
103-
byte[] buffer = new byte[4096];
104-
int len;
105-
while ((len = stream.read(buffer)) != -1) {
106-
result.write(buffer, 0, len);
107-
}
103+
ByteArrayOutputStream result = new ByteArrayOutputStream(size > 0 ? (int) size : InternalResource.DEFAULT_BUFFER_SIZE);
104+
InternalResource.transferTo(stream, result);
108105
return result.toByteArray();
109106
}
110107
}

java-io-base/src/test/java/_test/io/ResourceId.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package _test.io;
22

3+
import internal.io.InternalResource;
34
import internal.io.text.InternalTextResource;
45
import lombok.NonNull;
56

6-
import java.io.*;
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.Reader;
711
import java.nio.charset.Charset;
812
import java.nio.file.Files;
913
import java.nio.file.Path;
@@ -34,15 +38,9 @@ public Path copyTo(Path temp) throws IOException {
3438
}
3539

3640
public byte[] toBytes() throws IOException {
37-
ByteArrayOutputStream result = new ByteArrayOutputStream();
3841
try (InputStream input = open()) {
39-
byte[] buffer = new byte[8192];
40-
int read;
41-
while ((read = input.read(buffer, 0, buffer.length)) >= 0) {
42-
result.write(buffer, 0, read);
43-
}
42+
return InternalResource.readAllBytes(input);
4443
}
45-
return result.toByteArray();
4644
}
4745

4846
public BufferedReader open(Charset encoding) throws IOException {

0 commit comments

Comments
 (0)