Search in sources :

Example 71 with FileChannel

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;
}
Also used : FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException) DbusEventIterator(com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)

Example 72 with FileChannel

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();
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 73 with FileChannel

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;
    }
}
Also used : FileChannel(java.nio.channels.FileChannel)

Example 74 with FileChannel

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;
}
Also used : FileChannel(java.nio.channels.FileChannel)

Example 75 with FileChannel

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;
}
Also used : FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

FileChannel (java.nio.channels.FileChannel)676 IOException (java.io.IOException)247 ByteBuffer (java.nio.ByteBuffer)215 File (java.io.File)199 FileInputStream (java.io.FileInputStream)177 FileOutputStream (java.io.FileOutputStream)167 RandomAccessFile (java.io.RandomAccessFile)151 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)82 Path (java.nio.file.Path)42 FileLock (java.nio.channels.FileLock)34 FileNotFoundException (java.io.FileNotFoundException)32 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 OutputStream (java.io.OutputStream)12 ReadableByteChannel (java.nio.channels.ReadableByteChannel)12 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9