robtimus commented on a change in pull request #91:
URL: https://github.com/apache/commons-io/pull/91#discussion_r415379918



##########
File path: src/main/java/org/apache/commons/io/input/CharSequenceReader.java
##########
@@ -37,22 +37,107 @@
     private int idx;
     private int mark;
 
+    /*
+     * end is an Integer instead of int because of backwards compatibility.
+     * When de-serializing a CharSequenceReader that was serialized before
+     * these two fields were added, they will be initialized to 0 and null
+     * respectively. If end was an int, it would be initialized to 0 as well.
+     * That would cause all de-serialized CharSequenceReaders to be empty.
+     */
+    private final int start;
+    private final Integer end;
+
     /**
      * Construct a new instance with the specified character sequence.
      *
      * @param charSequence The character sequence, may be {@code null}
      */
     public CharSequenceReader(final CharSequence charSequence) {
+        this(charSequence, 0);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character 
sequence.
+     * <p>
+     * The start index is not strictly enforced to be within the bounds of the
+     * character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be 
thrown.
+     * Instead, if the character sequence grows smaller than the start index, 
this
+     * instance will act as if all characters have been read.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @throws IllegalArgumentException if the start index is negative
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start) {
+        this(charSequence, start, Integer.MAX_VALUE);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character 
sequence.
+     * <p>
+     * The start and end indexes are not strictly enforced to be within the 
bounds
+     * of the character sequence. This allows the character sequence to grow 
or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be 
thrown.
+     * Instead, if the character sequence grows smaller than the start index, 
this
+     * instance will act as if all characters have been read; if the character 
sequence
+     * grows smaller than the end, this instance will use the actual character 
sequence
+     * length.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @param end The start index in the character sequence, exclusive
+     * @throws IllegalArgumentException if the start index is negative, or if 
the end index is smaller than the start index
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start, int 
end) {
+        if (start < 0) {
+            throw new IllegalArgumentException(
+                    "Start index is less than zero: " + start);
+        }
+        if (end < start) {
+            throw new IllegalArgumentException(
+                    "End index is less than start " + start + ": " + end);
+        }
+        // Don't check the start and end indexes against the CharSequence,
+        // to let it grow and shrink without breaking existing behavior.
+
         this.charSequence = charSequence != null ? charSequence : "";
+        this.start = start;
+        this.end = end;
+
+        this.idx = start;
+        this.mark = start;
+    }
+
+    /**
+     * @return The start index in the character sequence, taking into account 
its length.

Review comment:
       I've added a different comment as the method doesn't start anything, 
making it clearer what the method returns.

##########
File path: src/main/java/org/apache/commons/io/input/CharSequenceReader.java
##########
@@ -37,22 +37,107 @@
     private int idx;
     private int mark;
 
+    /*
+     * end is an Integer instead of int because of backwards compatibility.
+     * When de-serializing a CharSequenceReader that was serialized before
+     * these two fields were added, they will be initialized to 0 and null
+     * respectively. If end was an int, it would be initialized to 0 as well.
+     * That would cause all de-serialized CharSequenceReaders to be empty.
+     */
+    private final int start;
+    private final Integer end;
+
     /**
      * Construct a new instance with the specified character sequence.
      *
      * @param charSequence The character sequence, may be {@code null}
      */
     public CharSequenceReader(final CharSequence charSequence) {
+        this(charSequence, 0);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character 
sequence.
+     * <p>
+     * The start index is not strictly enforced to be within the bounds of the
+     * character sequence. This allows the character sequence to grow or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be 
thrown.
+     * Instead, if the character sequence grows smaller than the start index, 
this
+     * instance will act as if all characters have been read.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @throws IllegalArgumentException if the start index is negative
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start) {
+        this(charSequence, start, Integer.MAX_VALUE);
+    }
+
+    /**
+     * Construct a new instance with a portion of the specified character 
sequence.
+     * <p>
+     * The start and end indexes are not strictly enforced to be within the 
bounds
+     * of the character sequence. This allows the character sequence to grow 
or shrink
+     * in size without risking any {@link IndexOutOfBoundsException} to be 
thrown.
+     * Instead, if the character sequence grows smaller than the start index, 
this
+     * instance will act as if all characters have been read; if the character 
sequence
+     * grows smaller than the end, this instance will use the actual character 
sequence
+     * length.
+     * </p>
+     *
+     * @param charSequence The character sequence, may be {@code null}
+     * @param start The start index in the character sequence, inclusive
+     * @param end The start index in the character sequence, exclusive
+     * @throws IllegalArgumentException if the start index is negative, or if 
the end index is smaller than the start index
+     * @since 2.7
+     */
+    public CharSequenceReader(final CharSequence charSequence, int start, int 
end) {
+        if (start < 0) {
+            throw new IllegalArgumentException(
+                    "Start index is less than zero: " + start);
+        }
+        if (end < start) {
+            throw new IllegalArgumentException(
+                    "End index is less than start " + start + ": " + end);
+        }
+        // Don't check the start and end indexes against the CharSequence,
+        // to let it grow and shrink without breaking existing behavior.
+
         this.charSequence = charSequence != null ? charSequence : "";
+        this.start = start;
+        this.end = end;
+
+        this.idx = start;
+        this.mark = start;
+    }
+
+    /**
+     * @return The start index in the character sequence, taking into account 
its length.
+     */
+    private int start() {
+        return Math.min(charSequence.length(), start);
+    }
+
+    /**
+     * @return The end index in the character sequence, taking into account 
its length.

Review comment:
       I've added a different comment as the method doesn't end anything, 
making it clearer what the method returns.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to