• ■ ■ ■ ■ ■ ■
    commons-utils/src/main/java/io/onedev/commons/utils/command/LineConsumer.java
    skipped 26 lines
    27 27  
    28 28   private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    29 29  
    30  - private boolean reset = false;
    31  -
    32 30   private String encoding;
    33 31  
    34 32   public LineConsumer(String encoding) {
    skipped 12 lines
    47 45   public void write(int b) throws IOException {
    48 46   byte c = (byte) b;
    49 47   if (c == '\n') {
    50  - processBuffer();
    51  - reset = false;
    52  - } else if (c == '\r') {
    53  - reset = true;
     48 + String line = getLine();
     49 + if (line.length() != 0 && line.charAt(line.length()-1) == '\r')
     50 + line = line.substring(0, line.length()-1);
     51 + consume(line);
    54 52   } else {
    55  - if (reset) {
    56  - buffer.reset();
    57  - reset = false;
    58  - }
    59 53   buffer.write(b);
    60 54   }
    61 55   }
    62 56   
    63  - protected void processBuffer() {
     57 + public abstract void consume(String line);
     58 + 
     59 + private String getLine() {
    64 60   try {
    65  - consume(encoding!=null?buffer.toString(encoding):buffer.toString());
     61 + String line = encoding!=null?buffer.toString(encoding):buffer.toString();
    66 62   buffer.reset();
     63 + return line;
    67 64   } catch (UnsupportedEncodingException e) {
    68 65   throw new RuntimeException(e);
    69 66   }
    70  - }
    71  - 
    72  - public abstract void consume(String line);
     67 + }
    73 68  
    74 69   @Override
    75 70   public void flush() throws IOException {
    76 71   if (buffer.size() > 0)
    77  - processBuffer();
     72 + consume(getLine());
    78 73   super.flush();
    79 74   }
    80 75   
    skipped 8 lines
Please wait...
Page is in error, reload to recover