Add TimetampedObserver.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -163,6 +163,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Make ObservableInputStream.getObservers() public.
</action>
+ <action issue="IO-706" dev="ggregory" type="add" due-to="Gary Gregory">
+ Add TimetampedObserver.
+ </action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Dependabot">
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

ADD
src/main/java/org/apache/commons/io/input/TimetampedObserver.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.input;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.time.Instant;
+
+import org.apache.commons.io.input.ObservableInputStream.Observer;
+
+/**
+ * An observer with timestamps.
+ * <p>
+ * For example:
+ * </p>
+ *
+ * <pre>
+ * final TimetampedObserver timetampedObserver = new TimetampedObserver();
+ * try (final ObservableInputStream inputStream = new ObservableInputStream(...),
+ * timetampedObserver)) {
+ * ...
+ * }
+ * System.out.printf("IO duration: %s%n", timetampedObserver.getOpenToCloseDuration());
+ * </pre>
+ *
+ * @since 2.9.0
+ */
+public class TimetampedObserver extends Observer {
+
+ private volatile Instant closeInstant;
+ private final Instant openInstant = Instant.now();
+
+ @Override
+ public void closed() throws IOException {
+ closeInstant = Instant.now();
+ }
+
+ /**
+ * Gets the instant for when this instance was closed.
+ *
+ * @return the instant for when closed was called.
+ */
+ public Instant getCloseInstant() {
+ return closeInstant;
+ }
+
+ /**
+ * Gets the Duration between creation and close.
+ *
+ * @return the Duration between creation and close.
+ */
+ public Duration getOpenToCloseDuration() {
+ return Duration.between(openInstant, closeInstant);
+ }
+
+ /**
+ * Gets the Duration between creation and now.
+ *
+ * @return the Duration between creation and now.
+ */
+ public Duration getOpenToNowDuration() {
+ return Duration.between(openInstant, Instant.now());
+ }
+
+ /**
+ * Gets the instant for when this instance was created.
+ *
+ * @return the instant for when this instance was created.
+ */
+ public Instant getOpenInstant() {
+ return openInstant;
+ }
+
+}

ADD
src/test/java/org/apache/commons/io/input/TimetampedObserverTest.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.input;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.time.Instant;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link TimetampedObserver}.
+ */
+public class TimetampedObserverTest {
+
+ @Test
+ public void test() throws IOException, InterruptedException {
+ final Instant before = Instant.now();
+ Thread.sleep(20); // Some OS' clock granularity may be high.
+ final TimetampedObserver timetampedObserver = new TimetampedObserver();
+ assertTrue(timetampedObserver.getOpenInstant().isAfter(before));
+ assertTrue(timetampedObserver.getOpenToNowDuration().toNanos() > 0);
+ assertNull(timetampedObserver.getCloseInstant());
+ final byte[] buffer = MessageDigestCalculatingInputStreamTest
+ .generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
+ try (final ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer),
+ timetampedObserver)) {
+ assertTrue(timetampedObserver.getOpenInstant().isAfter(before));
+ assertTrue(timetampedObserver.getOpenToNowDuration().toNanos() > 0);
+ }
+ assertTrue(timetampedObserver.getOpenInstant().isAfter(before));
+ assertTrue(timetampedObserver.getOpenToNowDuration().toNanos() > 0);
+ assertTrue(timetampedObserver.getCloseInstant().isAfter(timetampedObserver.getOpenInstant()));
+ assertTrue(timetampedObserver.getOpenToCloseDuration().toNanos() > 0);
+ }
+
+ @Test
+ public void testExample() throws IOException, InterruptedException {
+ final TimetampedObserver timetampedObserver = new TimetampedObserver();
+ final byte[] buffer = MessageDigestCalculatingInputStreamTest
+ .generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
+ try (final ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer),
+ timetampedObserver)) {
+ //
+ }
+ System.out.printf("IO duration: %s%n", timetampedObserver.getOpenToCloseDuration());
+ }
+
+}

[IO-706] Add TimetampedObserver.