Add and use a package-private factory for UnsupportedOperationException
to provide exception messages with a consistent formatting.
MODIFY
src/main/java/org/apache/commons/io/LineIterator.java
src/main/java/org/apache/commons/io/LineIterator.java
@@ -168,7 +168,7 @@ public void close() throws IOException {
*/
@Override
public void remove() {
- throw new UnsupportedOperationException("Remove unsupported on LineIterator");
+ throw new UnsupportedOperationException("remove not supported");
}
//-----------------------------------------------------------------------

MODIFY
src/main/java/org/apache/commons/io/input/NullInputStream.java
src/main/java/org/apache/commons/io/input/NullInputStream.java
@@ -167,7 +167,7 @@ public void close() throws IOException {
@Override
public synchronized void mark(final int readlimit) {
if (!markSupported) {
- throw new UnsupportedOperationException("Mark not supported");
+ throw UnsupportedOperationExceptions.mark();
}
mark = position;
this.readlimit = readlimit;
@@ -263,7 +263,7 @@ public int read(final byte[] bytes, final int offset, final int length) throws I
@Override
public synchronized void reset() throws IOException {
if (!markSupported) {
- throw new UnsupportedOperationException("Mark not supported");
+ throw UnsupportedOperationExceptions.reset();
}
if (mark < 0) {
throw new IOException("No position has been marked");

MODIFY
src/main/java/org/apache/commons/io/input/NullReader.java
src/main/java/org/apache/commons/io/input/NullReader.java
@@ -152,7 +152,7 @@ public void close() throws IOException {
@Override
public synchronized void mark(final int readlimit) {
if (!markSupported) {
- throw new UnsupportedOperationException("Mark not supported");
+ throw UnsupportedOperationExceptions.mark();
}
mark = position;
this.readlimit = readlimit;
@@ -248,7 +248,7 @@ public int read(final char[] chars, final int offset, final int length) throws I
@Override
public synchronized void reset() throws IOException {
if (!markSupported) {
- throw new UnsupportedOperationException("Mark not supported");
+ throw UnsupportedOperationExceptions.reset();
}
if (mark < 0) {
throw new IOException("No position has been marked");

MODIFY
src/main/java/org/apache/commons/io/input/SwappedDataInputStream.java
src/main/java/org/apache/commons/io/input/SwappedDataInputStream.java
@@ -162,7 +162,7 @@ public int readInt() throws IOException, EOFException {
*/
@Override
public String readLine() throws IOException, EOFException {
- throw new UnsupportedOperationException("Operation not supported: readLine()");
+ throw UnsupportedOperationExceptions.method("readLine");
}
/**
@@ -222,7 +222,7 @@ public int readUnsignedShort() throws IOException, EOFException {
*/
@Override
public String readUTF() throws IOException, EOFException {
- throw new UnsupportedOperationException("Operation not supported: readUTF()");
+ throw UnsupportedOperationExceptions.method("readUTF");
}
/**

MODIFY
src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
@@ -122,6 +122,6 @@ public void close() throws IOException {
*/
@Override
public synchronized void mark(final int readlimit) {
- throw new UnsupportedOperationException("Mark not supported");
+ throw UnsupportedOperationExceptions.mark();
}
}

ADD
src/main/java/org/apache/commons/io/input/UnsupportedOperationExceptions.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;
+
+/**
+ * Package-private factory for {@link UnsupportedOperationException} to provide messages with consistent formatting.
+ *
+ * TODO Consider making this public and use from LineIterator.
+ */
+class UnsupportedOperationExceptions {
+
+ private static final String MARK_RESET = "mark/reset";
+
+ /**
+ * Creates a new instance of UnsupportedOperationException for a {@code mark} method.
+ *
+ * @return a new instance of UnsupportedOperationException
+ */
+ static UnsupportedOperationException mark() {
+ // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1.
+ return method(MARK_RESET);
+ }
+
+ /**
+ * Creates a new instance of UnsupportedOperationException for the given unsupported a {@code method} name.
+ *
+ * @param method A method name
+ * @return a new instance of UnsupportedOperationException
+ */
+ static UnsupportedOperationException method(final String method) {
+ return new UnsupportedOperationException(method + " not supported");
+ }
+
+ /**
+ * Creates a new instance of UnsupportedOperationException for a {@code reset} method.
+ *
+ * @return a new instance of UnsupportedOperationException
+ */
+ static UnsupportedOperationException reset() {
+ // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1.
+ return method(MARK_RESET);
+ }
+}

MODIFY
src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
@@ -130,6 +130,6 @@ public void close() throws IOException {
*/
@Override
public synchronized void mark(final int readlimit) {
- throw new UnsupportedOperationException("Mark not supported");
+ throw UnsupportedOperationExceptions.mark();
}
}

MODIFY
src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
*/
public class NullInputStreamTest {
+ // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1.
+ private static final String MARK_RESET_NOT_SUPPORTED = "mark/reset not supported";
+
@Test
public void testRead() throws Exception {
final int size = 5;
@@ -178,14 +181,14 @@ public void testMarkNotSupported() throws Exception {
input.mark(5);
fail("mark() should throw UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
- assertEquals("Mark not supported", e.getMessage(), "mark() error message");
+ assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "mark() error message");
}
try {
input.reset();
fail("reset() should throw UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
- assertEquals("Mark not supported", e.getMessage(), "reset() error message");
+ assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "reset() error message");
}
input.close();
}

MODIFY
src/test/java/org/apache/commons/io/input/NullReaderTest.java
src/test/java/org/apache/commons/io/input/NullReaderTest.java
*/
public class NullReaderTest {
+ // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1.
+ private static final String MARK_RESET_NOT_SUPPORTED = "mark/reset not supported";
+
@Test
public void testRead() throws Exception {
final int size = 5;
@@ -175,14 +178,14 @@ public void testMarkNotSupported() throws Exception {
reader.mark(5);
fail("mark() should throw UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
- assertEquals("Mark not supported", e.getMessage(), "mark() error message");
+ assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "mark() error message");
}
try {
reader.reset();
fail("reset() should throw UnsupportedOperationException");
} catch (final UnsupportedOperationException e) {
- assertEquals("Mark not supported", e.getMessage(), "reset() error message");
+ assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "reset() error message");
}
reader.close();
}

Add and use a package-private factory for UnsupportedOperationException
to provide exception messages with a consistent formatting.