ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From umag...@apache.org
Subject cvs commit: jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters StripLineBreaks.java
Date Sat, 16 Feb 2002 22:24:52 GMT
umagesh     02/02/16 14:24:52

  Modified:    proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters
                        StripLineBreaks.java
  Log:
  Operate at the read() method level instead of at the read(char[]) method level.
  
  Revision  Changes    Path
  1.3       +47 -48    jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java
  
  Index: StripLineBreaks.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StripLineBreaks.java	16 Feb 2002 02:29:34 -0000	1.2
  +++ StripLineBreaks.java	16 Feb 2002 22:24:52 -0000	1.3
  @@ -30,6 +30,10 @@
   
       private Parameter[] parameters;
   
  +    private String lineBreaks = DEFAULT_LINE_BREAKS;
  +
  +    private boolean initialized = false;
  +
       /**
        * Create a new filtered reader.
        *
  @@ -39,64 +43,59 @@
           super(in);
       }
   
  -    /**
  -     * Strip line break characters from an array.
  -     *
  -     * @exception  IOException  If an I/O error occurs
  -     */
  -    public final int read(final char[] cbuf) throws IOException {
  -        int length = -1;
  -        if (cbuf != null) {
  -            length = cbuf.length;
  -            if (in != null) {
  -                length = in.read(cbuf);
  -            }
  -            if (length != -1) {
  -                String str = new String(cbuf, 0, length);
  -                str = stripLineBreaks(str);
  -                final char[] newcbuf = str.toCharArray();
  -                System.arraycopy(newcbuf, 0, cbuf, 0, newcbuf.length);
  -                for (int j = newcbuf.length; j < cbuf.length; j++) {
  -                    cbuf[j] = 0;
  +    public final int read() throws IOException {
  +        if (!initialized) {
  +            String userDefinedLineBreaks = null;
  +            if (parameters != null) {
  +                for (int i = 0; i < parameters.length; i++) {
  +                    if (LINE_BREAKS_KEY.equals(parameters[i].getName())) {
  +                        userDefinedLineBreaks = parameters[i].getValue();
  +                        break;
  +                    }
                   }
  -                length = newcbuf.length;
               }
  +
  +            if (userDefinedLineBreaks != null) {
  +                lineBreaks = userDefinedLineBreaks;
  +            }
  +
  +            initialized = true;
           }
  -        return length;
  +
  +        int ch = in.read();
  +        while (ch != -1) {
  +            if (lineBreaks.indexOf(ch) == -1) {
  +                break;
  +            } else {
  +                ch = in.read();
  +            }
  +        }
  +        return ch;
       }
   
  -    /**
  -     * strip out all line breaks from a string.
  -     * @param source source
  -     * This implementation always duplicates the string; it is nominally possible to probe
  -     * the string first looking for any line breaks before bothering to do a copy. But
we assume if
  -     * the option is requested, then line breaks are probably in the source string.
  -     */
  -    private final String stripLineBreaks(final String source) {
  -        final int len = source.length();
  -        String userDefinedLineBreaks = null;
  -        if (parameters != null) {
  -            for (int i = 0; i < parameters.length; i++) {
  -                if (LINE_BREAKS_KEY.equals(parameters[i].getName())) {
  -                    userDefinedLineBreaks = parameters[i].getValue();
  -                    break;
  +    public final int read(final char cbuf[], final int off,
  +                          final int len) throws IOException {
  +        for (int i = 0; i < len; i++) {
  +            final int ch = read();
  +            if (ch == -1) {
  +                if (i == 0) {
  +                    return -1;
  +                } else {
  +                    return i;
                   }
               }
  +            cbuf[off + i] = (char) ch;
           }
  +        return len;
  +    }
   
  -        String lineBreaks = DEFAULT_LINE_BREAKS;
  -        if (userDefinedLineBreaks != null) {
  -            lineBreaks = userDefinedLineBreaks;
  -        }
  -        final StringBuffer dest = new StringBuffer(len);
  -        for(int i=0;i<len;++i) {
  -            final char ch=source.charAt(i);
  -            if(lineBreaks.indexOf(ch)==-1) {
  -                dest.append(ch);
  +    public final long skip(long n) throws IOException {
  +        for (long i = 0; i < n; i++) {
  +            if (in.read() == -1) {
  +                return i;
               }
           }
  -        return new String(dest);
  -
  +        return n;
       }
   
       /**
  @@ -105,4 +104,4 @@
       public final void setParameters(final Parameter[] parameters) {
           this.parameters = parameters;
       }
  -}
  \ No newline at end of file
  +}
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message