use of java.nio.channels.FileChannel in project databus by linkedin.
the class EventLogReader method read.
public Checkpoint read() {
Checkpoint checkPoint = new Checkpoint();
checkPoint.setFlexible();
if (_enabled) {
// ArrayList<InternalDatabusEventsListener> eventListeners = new ArrayList<InternalDatabusEventsListener>();
_eventBuffer.addInternalListener(checkPoint);
_eventBuffer.addInternalListener(this);
if (_filesToRead != null) {
for (File f : _filesToRead) {
FileChannel readChannel = null;
try {
readChannel = new FileInputStream(f).getChannel();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
int numEvents = 0;
try {
numEvents = _eventBuffer.readEvents(readChannel, getEncoding(f));
} catch (InvalidEventException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LOG.info("Read " + numEvents + " events");
}
_eventBuffer.removeInternalListener(checkPoint);
_eventBuffer.removeInternalListener(this);
LOG.info("Checkpoint = " + checkPoint);
if (_eventSeen) {
DbusEventIterator iter = _eventBuffer.acquireIterator("EventLogReader:firstEvent");
try {
DbusEvent event = iter.next();
String firstEventContent = event.toString();
// if we didn't wrap the buffer, and the first event is not an eop marker, delete the first window
if (_firstEventContent.equalsIgnoreCase(firstEventContent) && !event.isEndOfPeriodMarker()) {
long result = _eventBuffer.deleteFirstWindow();
if (result < 0) {
throw new RuntimeException("eventBuffer could not delete first window");
}
}
} finally {
_eventBuffer.releaseIterator(iter);
}
if (_lastEopOffset >= 0) {
iter = _eventBuffer.new DbusEventIterator(this._lastEopOffset, _eventBuffer.getTail(), "EventLogReader:lastEOP");
try {
DbusEvent event = iter.next();
if (!event.isEndOfPeriodMarker()) {
throw new RuntimeException("Tried reading end of period marker, but failed");
}
if (iter.hasNext()) {
_eventBuffer.setTail(iter.getCurrentPosition());
}
} catch (NoSuchElementException e) {
LOG.error("NoSuchElementException at : " + _eventBuffer.getBufferPositionParser().toString(_lastEopOffset));
throw e;
} finally {
_eventBuffer.releaseIterator(iter);
}
}
}
}
}
return checkPoint;
}
use of java.nio.channels.FileChannel in project blade by biezhi.
the class FileKit method copy.
/**
* 复制单个文件
* @param sourceFile 准备复制的文件源
* @param destFile 拷贝到新绝对路径带文件名
* @return
* @throws IOException
*/
@SuppressWarnings("resource")
public static void copy(String sourceFile, String destFile) throws IOException {
File source = new File(sourceFile);
if (source.exists()) {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
File dest = new File(destFile);
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
}
use of java.nio.channels.FileChannel in project h2o-2 by h2oai.
the class PersistNFS method load.
// Read up to 'len' bytes of Value. Value should already be persisted to
// disk. A racing delete can trigger a failure where we get a null return,
// but no crash (although one could argue that a racing load&delete is a bug
// no matter what).
@Override
public byte[] load(Value v) {
long skip = 0;
Key k = v._key;
// Convert a chunk into a long-offset from the base file.
if (k._kb[0] == Key.DVEC)
// The offset
skip = water.fvec.NFSFileVec.chunkOffset(k);
try {
FileInputStream s = null;
try {
s = new FileInputStream(getFileForKey(k));
FileChannel fc = s.getChannel();
fc.position(skip);
AutoBuffer ab = new AutoBuffer(fc, true, Value.NFS);
byte[] b = ab.getA1(v._max);
ab.close();
assert v.isPersisted();
return b;
} finally {
if (s != null)
s.close();
}
} catch (IOException e) {
// Broken disk / short-file???
H2O.ignore(e);
return null;
}
}
use of java.nio.channels.FileChannel in project HanLP by hankcs.
the class IOUtil method saveTxt.
/**
* 快速保存
*
* @param path
* @param content
* @return
*/
public static boolean saveTxt(String path, String content) {
try {
FileChannel fc = new FileOutputStream(path).getChannel();
fc.write(ByteBuffer.wrap(content.getBytes()));
fc.close();
} catch (Exception e) {
logger.throwing("IOUtil", "saveTxt", e);
logger.warning("IOUtil saveTxt 到" + path + "失败" + e.toString());
return false;
}
return true;
}
use of java.nio.channels.FileChannel in project HanLP by hankcs.
the class IOUtil method readBytesFromFileInputStream.
private static byte[] readBytesFromFileInputStream(FileInputStream fis) throws IOException {
FileChannel channel = fis.getChannel();
int fileSize = (int) channel.size();
ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
channel.read(byteBuffer);
byteBuffer.flip();
byte[] bytes = byteBuffer.array();
byteBuffer.clear();
channel.close();
fis.close();
return bytes;
}
Aggregations