2018-12-01 09:52:17 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
2021-07-10 20:50:53 +00:00
|
|
|
* Copyright (c) 2016-2020 FabricMC
|
2018-12-01 09:52:17 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-04-22 11:37:14 +00:00
|
|
|
package net.fabricmc.loom.util;
|
|
|
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by covers1624 on 20/12/18.
|
|
|
|
*/
|
|
|
|
public class ConsumingOutputStream extends OutputStream {
|
2019-11-02 20:23:27 +00:00
|
|
|
private final Consumer<String> consumer;
|
2019-04-22 11:37:14 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
private StringBuilder buffer = new StringBuilder();
|
2019-04-22 11:37:14 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
public ConsumingOutputStream(Consumer<String> consumer) {
|
|
|
|
this.consumer = consumer;
|
|
|
|
}
|
2019-04-22 11:37:14 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
@Override
|
2020-07-26 20:32:10 +00:00
|
|
|
public void write(int b) {
|
2019-11-02 20:23:27 +00:00
|
|
|
char ch = (char) (b & 0xFF);
|
|
|
|
buffer.append(ch);
|
2019-04-22 11:37:14 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
if (ch == '\n' || ch == '\r') {
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 11:37:14 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
@Override
|
2020-07-26 20:32:10 +00:00
|
|
|
public void flush() {
|
2019-11-02 20:23:27 +00:00
|
|
|
String str = buffer.toString();
|
|
|
|
|
|
|
|
if (str.endsWith("\r") || str.endsWith("\n")) {
|
|
|
|
str = str.trim();
|
|
|
|
|
|
|
|
if (!str.isEmpty()) {
|
|
|
|
consumer.accept(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = new StringBuilder();
|
|
|
|
}
|
|
|
|
}
|
2018-12-01 09:52:17 +00:00
|
|
|
}
|