Skip to content

Commit 5cabfc7

Browse files
committed
Sort members
1 parent f7f3e7d commit 5cabfc7

File tree

4 files changed

+161
-161
lines changed

4 files changed

+161
-161
lines changed

src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ private static CharsetEncoder newEncoder(final Charset charset) {
167167
private int cBufMark; // position in cBuf
168168
private final CharsetEncoder charsetEncoder;
169169

170+
private CharSequenceInputStream(final Builder builder) {
171+
this.charsetEncoder = builder.charsetEncoder;
172+
// Ensure that buffer is long enough to hold a complete character
173+
this.bBuf = ByteBuffer.allocate(ReaderInputStream.checkMinBufferSize(builder.charsetEncoder, builder.getBufferSize()));
174+
this.bBuf.flip();
175+
this.cBuf = CharBuffer.wrap(Uncheck.get(() -> builder.getCharSequence()));
176+
this.cBufMark = NO_MARK;
177+
this.bBufMark = NO_MARK;
178+
try {
179+
fillBuffer();
180+
} catch (final CharacterCodingException ex) {
181+
// Reset everything without filling the buffer
182+
// so the same exception can be thrown again later.
183+
this.bBuf.clear();
184+
this.bBuf.flip();
185+
this.cBuf.rewind();
186+
}
187+
}
188+
170189
/**
171190
* Constructs a new instance with a buffer size of {@link IOUtils#DEFAULT_BUFFER_SIZE}.
172191
*
@@ -194,25 +213,6 @@ public CharSequenceInputStream(final CharSequence cs, final Charset charset, fin
194213
this(builder().setCharSequence(cs).setCharset(charset).setBufferSize(bufferSize));
195214
}
196215

197-
private CharSequenceInputStream(final Builder builder) {
198-
this.charsetEncoder = builder.charsetEncoder;
199-
// Ensure that buffer is long enough to hold a complete character
200-
this.bBuf = ByteBuffer.allocate(ReaderInputStream.checkMinBufferSize(builder.charsetEncoder, builder.getBufferSize()));
201-
this.bBuf.flip();
202-
this.cBuf = CharBuffer.wrap(Uncheck.get(() -> builder.getCharSequence()));
203-
this.cBufMark = NO_MARK;
204-
this.bBufMark = NO_MARK;
205-
try {
206-
fillBuffer();
207-
} catch (final CharacterCodingException ex) {
208-
// Reset everything without filling the buffer
209-
// so the same exception can be thrown again later.
210-
this.bBuf.clear();
211-
this.bBuf.flip();
212-
this.cBuf.rewind();
213-
}
214-
}
215-
216216
/**
217217
* Constructs a new instance with a buffer size of {@link IOUtils#DEFAULT_BUFFER_SIZE}.
218218
*

src/test/java/org/apache/commons/io/FileUtilsTest.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,23 @@ void testForceDeleteAFileDoesNotExist() {
16171617

16181618
}
16191619

1620+
@Test
1621+
public void testForceDeleteBrokenSymlink() throws Exception {
1622+
final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir();
1623+
final Path symlinkedDir = pair.getLeft();
1624+
final Path targetDir = pair.getRight();
1625+
1626+
Files.delete(targetDir);
1627+
assertFalse(Files.exists(symlinkedDir));
1628+
assertTrue(Files.isSymbolicLink(symlinkedDir));
1629+
1630+
FileUtils.forceDelete(symlinkedDir.toFile());
1631+
1632+
// check targeted symlink is gone
1633+
assertFalse(Files.exists(symlinkedDir));
1634+
assertFalse(Files.isSymbolicLink(symlinkedDir));
1635+
}
1636+
16201637
@Test
16211638
void testForceDeleteDir() throws Exception {
16221639
final File testDirectory = tempDirFile;
@@ -1690,6 +1707,25 @@ void testForceDeleteReadOnlyFile() throws Exception {
16901707
}
16911708
}
16921709

1710+
@Test
1711+
public void testForceDeleteSymlink() throws Exception {
1712+
final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir();
1713+
final Path symlinkedDir = pair.getLeft();
1714+
final Path targetDir = pair.getRight();
1715+
1716+
assertTrue(Files.exists(symlinkedDir));
1717+
assertTrue(Files.isSymbolicLink(symlinkedDir));
1718+
assertTrue(Files.exists(targetDir));
1719+
1720+
FileUtils.forceDelete(symlinkedDir.toFile());
1721+
1722+
// check targeted symlink is gone
1723+
assertFalse(Files.exists(symlinkedDir));
1724+
assertFalse(Files.isSymbolicLink(symlinkedDir));
1725+
// dir targeted by symlink is not deleted
1726+
assertTrue(Files.exists(targetDir));
1727+
}
1728+
16931729
/**
16941730
* TODO Passes on macOS, fails on Linux and Windows with AccessDeniedException.
16951731
*/
@@ -1761,42 +1797,6 @@ void testForceDeleteUnwritableFile() throws Exception {
17611797
}
17621798
}
17631799

1764-
@Test
1765-
public void testForceDeleteBrokenSymlink() throws Exception {
1766-
final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir();
1767-
final Path symlinkedDir = pair.getLeft();
1768-
final Path targetDir = pair.getRight();
1769-
1770-
Files.delete(targetDir);
1771-
assertFalse(Files.exists(symlinkedDir));
1772-
assertTrue(Files.isSymbolicLink(symlinkedDir));
1773-
1774-
FileUtils.forceDelete(symlinkedDir.toFile());
1775-
1776-
// check targeted symlink is gone
1777-
assertFalse(Files.exists(symlinkedDir));
1778-
assertFalse(Files.isSymbolicLink(symlinkedDir));
1779-
}
1780-
1781-
@Test
1782-
public void testForceDeleteSymlink() throws Exception {
1783-
final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir();
1784-
final Path symlinkedDir = pair.getLeft();
1785-
final Path targetDir = pair.getRight();
1786-
1787-
assertTrue(Files.exists(symlinkedDir));
1788-
assertTrue(Files.isSymbolicLink(symlinkedDir));
1789-
assertTrue(Files.exists(targetDir));
1790-
1791-
FileUtils.forceDelete(symlinkedDir.toFile());
1792-
1793-
// check targeted symlink is gone
1794-
assertFalse(Files.exists(symlinkedDir));
1795-
assertFalse(Files.isSymbolicLink(symlinkedDir));
1796-
// dir targeted by symlink is not deleted
1797-
assertTrue(Files.exists(targetDir));
1798-
}
1799-
18001800
@Test
18011801
void testForceMkdir() throws Exception {
18021802
// Tests with existing directory
@@ -2826,12 +2826,12 @@ void testReadFileToStringWithEncoding() throws Exception {
28262826
}
28272827

28282828
@Test
2829-
void testReadLinesUTF8() throws Exception {
2830-
final File file = TestUtils.newFile(tempDirFile, "lines.txt");
2831-
final String[] data = { "hello", "\u1234", "", "this is", "some text" };
2832-
TestUtils.createLineFileUtf8(file, data);
2833-
final List<String> lines = FileUtils.readLines(file, UTF_8);
2834-
assertEquals(Arrays.asList(data), lines);
2829+
@EnabledIf("isPosixFilePermissionsSupported")
2830+
void testReadLines_IOExceptionOnPosixFileSystem() throws Exception {
2831+
final File file = TestUtils.newFile(tempDirFile, "cant-read.txt");
2832+
TestUtils.createFile(file, 100);
2833+
Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("---------"));
2834+
assertThrows(IOException.class, () -> FileUtils.readLines(file));
28352835
}
28362836

28372837
@Test
@@ -2856,12 +2856,12 @@ void testReadLinesErrors() {
28562856
}
28572857

28582858
@Test
2859-
@EnabledIf("isPosixFilePermissionsSupported")
2860-
void testReadLines_IOExceptionOnPosixFileSystem() throws Exception {
2861-
final File file = TestUtils.newFile(tempDirFile, "cant-read.txt");
2862-
TestUtils.createFile(file, 100);
2863-
Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("---------"));
2864-
assertThrows(IOException.class, () -> FileUtils.readLines(file));
2859+
void testReadLinesUTF8() throws Exception {
2860+
final File file = TestUtils.newFile(tempDirFile, "lines.txt");
2861+
final String[] data = { "hello", "\u1234", "", "this is", "some text" };
2862+
TestUtils.createLineFileUtf8(file, data);
2863+
final List<String> lines = FileUtils.readLines(file, UTF_8);
2864+
assertEquals(Arrays.asList(data), lines);
28652865
}
28662866

28672867
@Test

src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,45 @@ public static Stream<Arguments> inputData() {
8484
// @formatter:on
8585
}
8686

87+
@TestFactory
88+
public DynamicTest[] bulkReadErrorHandlingTests() {
89+
final QueueInputStream queueInputStream = new QueueInputStream();
90+
return new DynamicTest[] {
91+
dynamicTest("Offset too big", () ->
92+
assertThrows(IndexOutOfBoundsException.class, () ->
93+
queueInputStream.read(EMPTY_BYTE_ARRAY, 1, 0))),
94+
95+
dynamicTest("Offset negative", () ->
96+
assertThrows(IndexOutOfBoundsException.class, () ->
97+
queueInputStream.read(EMPTY_BYTE_ARRAY, -1, 0))),
98+
99+
dynamicTest("Length too big", () ->
100+
assertThrows(IndexOutOfBoundsException.class, () ->
101+
queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 1))),
102+
103+
dynamicTest("Length negative", () ->
104+
assertThrows(IndexOutOfBoundsException.class, () ->
105+
queueInputStream.read(EMPTY_BYTE_ARRAY, 0, -1))),
106+
};
107+
}
108+
87109
private int defaultBufferSize() {
88110
return 8192;
89111
}
90112

113+
private void doTestReadLineByLine(final String inputData, final InputStream inputStream, final OutputStream outputStream) throws IOException {
114+
final String[] lines = inputData.split("\n");
115+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8))) {
116+
for (final String line : lines) {
117+
outputStream.write(line.getBytes(UTF_8));
118+
outputStream.write('\n');
119+
120+
final String actualLine = reader.readLine();
121+
assertEquals(line, actualLine);
122+
}
123+
}
124+
}
125+
91126
private String readUnbuffered(final InputStream inputStream) throws IOException {
92127
return readUnbuffered(inputStream, Integer.MAX_VALUE);
93128
}
@@ -146,74 +181,30 @@ void testBufferedReads(final String inputData) throws IOException {
146181

147182
@ParameterizedTest(name = "inputData={0}")
148183
@MethodSource("inputData")
149-
void testReadLineByLineQueue(final String inputData) throws IOException {
150-
final String[] lines = inputData.split("\n");
184+
void testBufferedReadWrite(final String inputData) throws IOException {
151185
final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
152-
try (QueueInputStream inputStream = QueueInputStream.builder()
153-
.setBlockingQueue(queue)
154-
.setTimeout(Duration.ofHours(1))
155-
.get();
156-
QueueOutputStream outputStream = inputStream.newQueueOutputStream()) {
157-
158-
doTestReadLineByLine(inputData, inputStream, outputStream);
186+
try (BufferedInputStream inputStream = new BufferedInputStream(new QueueInputStream(queue));
187+
BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) {
188+
outputStream.write(inputData.getBytes(StandardCharsets.UTF_8));
189+
outputStream.flush();
190+
final String dataCopy = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
191+
assertEquals(inputData, dataCopy);
159192
}
160193
}
161194

162195
@ParameterizedTest(name = "inputData={0}")
163196
@MethodSource("inputData")
164-
void testReadLineByLineFile(final String inputData) throws IOException {
165-
final Path tempFile = Files.createTempFile(getClass().getSimpleName(), ".txt");
166-
try (InputStream inputStream = Files.newInputStream(tempFile);
167-
OutputStream outputStream = Files.newOutputStream(tempFile)) {
168-
169-
doTestReadLineByLine(inputData, inputStream, outputStream);
170-
} finally {
171-
Files.delete(tempFile);
172-
}
173-
}
174-
175-
private void doTestReadLineByLine(final String inputData, final InputStream inputStream, final OutputStream outputStream) throws IOException {
176-
final String[] lines = inputData.split("\n");
177-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8))) {
178-
for (final String line : lines) {
179-
outputStream.write(line.getBytes(UTF_8));
180-
outputStream.write('\n');
181-
182-
final String actualLine = reader.readLine();
183-
assertEquals(line, actualLine);
184-
}
197+
void testBufferedWrites(final String inputData) throws IOException {
198+
final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
199+
try (QueueInputStream inputStream = new QueueInputStream(queue);
200+
BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) {
201+
outputStream.write(inputData.getBytes(StandardCharsets.UTF_8));
202+
outputStream.flush();
203+
final String actualData = readUnbuffered(inputStream);
204+
assertEquals(inputData, actualData);
185205
}
186206
}
187207

188-
@TestFactory
189-
public DynamicTest[] bulkReadErrorHandlingTests() {
190-
final QueueInputStream queueInputStream = new QueueInputStream();
191-
return new DynamicTest[] {
192-
dynamicTest("Offset too big", () ->
193-
assertThrows(IndexOutOfBoundsException.class, () ->
194-
queueInputStream.read(EMPTY_BYTE_ARRAY, 1, 0))),
195-
196-
dynamicTest("Offset negative", () ->
197-
assertThrows(IndexOutOfBoundsException.class, () ->
198-
queueInputStream.read(EMPTY_BYTE_ARRAY, -1, 0))),
199-
200-
dynamicTest("Length too big", () ->
201-
assertThrows(IndexOutOfBoundsException.class, () ->
202-
queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 1))),
203-
204-
dynamicTest("Length negative", () ->
205-
assertThrows(IndexOutOfBoundsException.class, () ->
206-
queueInputStream.read(EMPTY_BYTE_ARRAY, 0, -1))),
207-
};
208-
}
209-
210-
@Test
211-
void testBulkReadZeroLength() {
212-
final QueueInputStream queueInputStream = new QueueInputStream();
213-
final int read = queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 0);
214-
assertEquals(0, read);
215-
}
216-
217208
@ParameterizedTest(name = "inputData={0}")
218209
@MethodSource("inputData")
219210
void testBulkReadWaiting(final String inputData) throws IOException {
@@ -256,30 +247,11 @@ public Integer poll(final long timeout, final TimeUnit unit) throws InterruptedE
256247
}
257248
}
258249

259-
@ParameterizedTest(name = "inputData={0}")
260-
@MethodSource("inputData")
261-
void testBufferedReadWrite(final String inputData) throws IOException {
262-
final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
263-
try (BufferedInputStream inputStream = new BufferedInputStream(new QueueInputStream(queue));
264-
BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) {
265-
outputStream.write(inputData.getBytes(StandardCharsets.UTF_8));
266-
outputStream.flush();
267-
final String dataCopy = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
268-
assertEquals(inputData, dataCopy);
269-
}
270-
}
271-
272-
@ParameterizedTest(name = "inputData={0}")
273-
@MethodSource("inputData")
274-
void testBufferedWrites(final String inputData) throws IOException {
275-
final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
276-
try (QueueInputStream inputStream = new QueueInputStream(queue);
277-
BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) {
278-
outputStream.write(inputData.getBytes(StandardCharsets.UTF_8));
279-
outputStream.flush();
280-
final String actualData = readUnbuffered(inputStream);
281-
assertEquals(inputData, actualData);
282-
}
250+
@Test
251+
void testBulkReadZeroLength() {
252+
final QueueInputStream queueInputStream = new QueueInputStream();
253+
final int read = queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 0);
254+
assertEquals(0, read);
283255
}
284256

285257
@Test
@@ -299,6 +271,34 @@ void testReadAfterClose(final String inputData) throws IOException {
299271
assertEquals(IOUtils.EOF, shadow.read());
300272
}
301273

274+
@ParameterizedTest(name = "inputData={0}")
275+
@MethodSource("inputData")
276+
void testReadLineByLineFile(final String inputData) throws IOException {
277+
final Path tempFile = Files.createTempFile(getClass().getSimpleName(), ".txt");
278+
try (InputStream inputStream = Files.newInputStream(tempFile);
279+
OutputStream outputStream = Files.newOutputStream(tempFile)) {
280+
281+
doTestReadLineByLine(inputData, inputStream, outputStream);
282+
} finally {
283+
Files.delete(tempFile);
284+
}
285+
}
286+
287+
@ParameterizedTest(name = "inputData={0}")
288+
@MethodSource("inputData")
289+
void testReadLineByLineQueue(final String inputData) throws IOException {
290+
final String[] lines = inputData.split("\n");
291+
final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
292+
try (QueueInputStream inputStream = QueueInputStream.builder()
293+
.setBlockingQueue(queue)
294+
.setTimeout(Duration.ofHours(1))
295+
.get();
296+
QueueOutputStream outputStream = inputStream.newQueueOutputStream()) {
297+
298+
doTestReadLineByLine(inputData, inputStream, outputStream);
299+
}
300+
}
301+
302302
@Test
303303
void testResetArguments() throws IOException {
304304
try (QueueInputStream queueInputStream = QueueInputStream.builder().setTimeout(null).get()) {

0 commit comments

Comments
 (0)