/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed 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 com.intellij.execution.junit;

import com.intellij.execution.junit2.SegmentedInputStream;

import java.io.IOException;
import java.io.Reader;

/**
 * @author Eugene Zhuravlev
*         Date: Apr 25, 2007
*/
public class SegmentedInputStreamReader extends Reader {
  private final SegmentedInputStream myStream;

  public SegmentedInputStreamReader(SegmentedInputStream stream) {
    myStream = stream;
  }

  public void close() throws IOException {
    myStream.close();
  }

  public boolean ready() throws IOException {
    return myStream.available() > 0;
  }

  public int read(final char[] cbuf, final int off, final int len) throws IOException {
    for (int i = 0; i < len; i++) {
      final int aChar = myStream.read();
      if (aChar == -1) return i == 0 ? -1 : i;
      cbuf[off + i] = (char)aChar;
    }
    return len;
  }
}

@@ -1264,145 +1264,275 @@
    f
-or (int i = 0; i %3C len; i++) %7B%0A      final int aChar = myStream.read();%0A      if (aChar == -1) return i == 0 ? -1 : i;%0A      cbuf%5Boff + i
+inal int maxBytes = Math.min(len, myStream.available());%0A    int bytesRead = 0;%0A    while (bytesRead %3C maxBytes) %7B%0A      final int aChar = myStream.read();%0A      if (aChar == -1) %7B%0A        return bytesRead == 0 ? -1 : bytesRead;%0A      %7D%0A      cbuf%5Boff + (bytesRead++)
 %5D = 
@@ -1561,19 +1561,25 @@
  return 
-len
+bytesRead
 ;%0A  %7D%0A%7D%0A

correct implementation of read()