use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class FileWriter method write.
// ------------------------------------------------------- Constructor end
/**
* 将String写入文件
*
* @param content 写入的内容
* @param isAppend 是否追加
* @return 目标文件
* @throws IORuntimeException IO异常
*/
public File write(String content, boolean isAppend) throws IORuntimeException {
BufferedWriter writer = null;
try {
writer = getWriter(isAppend);
writer.write(content);
writer.flush();
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(writer);
}
return file;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class FileWriter method writeFromStream.
/**
* 将流的内容写入文件
*
* @param in 输入流,不关闭
* @param isCloseIn 是否关闭输入流
* @return dest
* @throws IORuntimeException IO异常
* @since 5.5.2
*/
public File writeFromStream(InputStream in, boolean isCloseIn) throws IORuntimeException {
FileOutputStream out = null;
try {
out = new FileOutputStream(FileUtil.touch(file));
IoUtil.copy(in, out);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(out);
if (isCloseIn) {
IoUtil.close(in);
}
}
return file;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class Tailer method start.
/**
* 开始监听
*
* @param async 是否异步执行
*/
public void start(boolean async) {
// 初始读取
try {
this.readTail();
} catch (IOException e) {
throw new IORuntimeException(e);
}
final LineReadWatcher lineReadWatcher = new LineReadWatcher(this.randomAccessFile, this.charset, this.lineHandler);
final ScheduledFuture<?> scheduledFuture = //
this.executorService.scheduleAtFixedRate(//
lineReadWatcher, //
0, //
this.period, //
TimeUnit.MILLISECONDS);
if (false == async) {
try {
scheduledFuture.get();
} catch (ExecutionException e) {
throw new UtilException(e);
} catch (InterruptedException e) {
// ignore and exist
}
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class Tailer method readTail.
// ---------------------------------------------------------------------------------------- Private method start
/**
* 预读取行
*
* @throws IOException IO异常
*/
private void readTail() throws IOException {
final long len = this.randomAccessFile.length();
if (initReadLine > 0) {
Stack<String> stack = new Stack<>();
long start = this.randomAccessFile.getFilePointer();
long nextEnd = len - 1;
this.randomAccessFile.seek(nextEnd);
int c;
int currentLine = 0;
while (nextEnd > start) {
// 满
if (currentLine > initReadLine) {
break;
}
c = this.randomAccessFile.read();
if (c == CharUtil.LF || c == CharUtil.CR) {
// FileUtil.readLine(this.randomAccessFile, this.charset, this.lineHandler);
final String line = FileUtil.readLine(this.randomAccessFile, this.charset);
if (null != line) {
stack.push(line);
}
currentLine++;
nextEnd--;
}
nextEnd--;
this.randomAccessFile.seek(nextEnd);
if (nextEnd == 0) {
// 当文件指针退至文件开始处,输出第一行
// FileUtil.readLine(this.randomAccessFile, this.charset, this.lineHandler);
final String line = FileUtil.readLine(this.randomAccessFile, this.charset);
if (null != line) {
stack.push(line);
}
break;
}
}
// 输出缓存栈中的内容
while (false == stack.isEmpty()) {
this.lineHandler.handle(stack.pop());
}
}
// 将指针置于末尾
try {
this.randomAccessFile.seek(len);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class ChannelCopier method copy.
@Override
public long copy(ReadableByteChannel source, WritableByteChannel target) {
Assert.notNull(source, "InputStream is null !");
Assert.notNull(target, "OutputStream is null !");
final StreamProgress progress = this.progress;
if (null != progress) {
progress.start();
}
final long size;
try {
size = doCopy(source, target, ByteBuffer.allocate(bufferSize(this.count)), progress);
} catch (IOException e) {
throw new IORuntimeException(e);
}
if (null != progress) {
progress.finish();
}
return size;
}
Aggregations