Skip to content

Commit ec50f29

Browse files
committed
Add AbstractOrigin.size()
1 parent 921ad07 commit ec50f29

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ The <action> type attribute can be add,update,fix,remove.
8686
<action dev="ggregory" type="add" due-to="Gary Gregory">
8787
Add and use RandomAccessFiles.
8888
</action>
89+
<action dev="ggregory" type="add" due-to="Gary Gregory">
90+
Add AbstractOrigin.size().
91+
</action>
8992
<!-- UPDATE -->
9093
<action dev="ggregory" type="update" due-to="Gary Gregory, Dependabot">
9194
Bump commons-parent from 57 to 58.

src/main/java/org/apache/commons/io/build/AbstractOrigin.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public Reader getReader(final Charset charset) throws IOException {
8686
return new InputStreamReader(getInputStream(), charset);
8787
}
8888

89+
@Override
90+
public long size() throws IOException {
91+
return origin.length;
92+
}
93+
8994
}
9095

9196
/**
@@ -127,6 +132,11 @@ public Reader getReader(final Charset charset) throws IOException {
127132
return new InputStreamReader(getInputStream(), charset);
128133
}
129134

135+
@Override
136+
public long size() throws IOException {
137+
return origin.length();
138+
}
139+
130140
}
131141

132142
/**
@@ -501,6 +511,17 @@ public Writer getWriter(final Charset charset, final OpenOption... options) thro
501511
return Files.newBufferedWriter(getPath(), charset, options);
502512
}
503513

514+
/**
515+
* Gets the size of the origin, if possible.
516+
*
517+
* @return the size of the origin in bytes or characters.
518+
* @throws IOException if an I/O error occurs.
519+
* @since 2.13.0
520+
*/
521+
public long size() throws IOException {
522+
return Files.size(getPath());
523+
}
524+
504525
@Override
505526
public String toString() {
506527
return origin.toString();

src/test/java/org/apache/commons/io/build/AbstractOriginTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.io.build;
1818

1919
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122

2223
import java.io.IOException;
@@ -124,4 +125,9 @@ public void testGetWriter() throws IOException {
124125
assertNotNull(writer);
125126
}
126127
}
128+
129+
@Test
130+
public void testSize() throws IOException {
131+
assertEquals(Files.size(Paths.get(FILE_NAME_RO)), getOriginRo().getByteArray().length);
132+
}
127133
}

src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,11 @@ public void testGetReader() {
102102
assertThrows(UnsupportedOperationException.class, super::testGetReader);
103103
}
104104

105+
@Override
106+
@Test
107+
public void testSize() {
108+
// Cannot convert a Writer to a size.
109+
assertThrows(UnsupportedOperationException.class, super::testSize);
110+
}
111+
105112
}

src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,11 @@ public void testGetReader() {
101101
assertThrows(UnsupportedOperationException.class, super::testGetReader);
102102
}
103103

104+
@Override
105+
@Test
106+
public void testSize() {
107+
// Cannot convert a Writer to a size.
108+
assertThrows(UnsupportedOperationException.class, super::testSize);
109+
}
110+
104111
}

0 commit comments

Comments
 (0)