Java Code Examples for java.nio.channels.FileChannel#force()

The following examples show how to use java.nio.channels.FileChannel#force() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultMessageStoreTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void damageCommitlog(long offset) throws Exception {
    MessageStoreConfig messageStoreConfig = new MessageStoreConfig();
    File file = new File(messageStoreConfig.getStorePathCommitLog() + File.separator + "00000000000000000000");

    FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024 * 1024 * 10);

    int bodyLen = mappedByteBuffer.getInt((int) offset + 84);
    int topicLenIndex = (int) offset + 84 + bodyLen + 4;
    mappedByteBuffer.position(topicLenIndex);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);
    mappedByteBuffer.putInt(0);

    mappedByteBuffer.force();
    fileChannel.force(true);
    fileChannel.close();
}
 
Example 2
Source File: IOUtil.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
/**
 * writeFile
 * @param in data
 * @param targetPath targetPath
 * @param targetFileName targetFileName
 * @param append Whether to concatenate old data
 * @throws IOException IOException
 * @return File
 */
public static File writeFile(InputStream in, String targetPath, String targetFileName, boolean append) throws IOException {
    if(targetPath == null){
        targetPath = "";
    }
    File parent = new File(targetPath);
    parent.mkdirs();
    File outFile = new File(parent,targetFileName);
    if(!outFile.exists()){
        outFile.createNewFile();
    }
    FileChannel outChannel = new FileOutputStream(outFile,append).getChannel();
    long writeBeginIndex = append? outChannel.size() : 0L;
    ReadableByteChannel inChannel = Channels.newChannel(in);
    FileLock lock = outChannel.lock(writeBeginIndex,Long.MAX_VALUE - writeBeginIndex,false);
    try{
        outChannel.transferFrom(inChannel,writeBeginIndex,Long.MAX_VALUE);
    }finally {
        lock.release();
        outChannel.force(FORCE_META_DATA);
        inChannel.close();
        outChannel.close();
    }
    return outFile;
}
 
Example 3
Source File: FileIOUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 通过 FileChannel 把字节流写入文件
 * @param file    文件
 * @param bytes   byte[]
 * @param append  是否追加到结尾
 * @param isForce 是否强制写入
 * @return {@code true} success, {@code false} fail
 */
public static boolean writeFileFromBytesByChannel(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
    if (bytes == null || !FileUtils.createOrExistsFile(file)) return false;
    FileChannel fc = null;
    try {
        fc = new FileOutputStream(file, append).getChannel();
        fc.position(fc.size());
        fc.write(ByteBuffer.wrap(bytes));
        if (isForce) fc.force(true);
        return true;
    } catch (IOException e) {
        JCLogUtils.eTag(TAG, e, "writeFileFromBytesByChannel");
        return false;
    } finally {
        CloseUtils.closeIOQuietly(fc);
    }
}
 
Example 4
Source File: FileIOUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 通过 FileChannel 把字节流写入文件
 * @param file    文件
 * @param bytes   byte[]
 * @param append  是否追加到结尾
 * @param isForce 是否强制写入
 * @return {@code true} success, {@code false} fail
 */
public static boolean writeFileFromBytesByChannel(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
    if (bytes == null || !FileUtils.createOrExistsFile(file)) return false;
    FileChannel fc = null;
    try {
        fc = new FileOutputStream(file, append).getChannel();
        fc.position(fc.size());
        fc.write(ByteBuffer.wrap(bytes));
        if (isForce) fc.force(true);
        return true;
    } catch (IOException e) {
        JCLogUtils.eTag(TAG, e, "writeFileFromBytesByChannel");
        return false;
    } finally {
        CloseUtils.closeIOQuietly(fc);
    }
}
 
Example 5
Source File: FileUtil.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Copies data from the input channel to the output file channel.
 *
 * @param input  the input channel to copy.
 * @param output the output channel to copy.
 * @throws IOException if there is an I/O error.
 */
@SuppressLint("LambdaLast")
public static void copy(@NonNull ReadableByteChannel input, @NonNull FileChannel output)
        throws IOException {
    try {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            output.transferFrom(input, 0, Long.MAX_VALUE);
        } else {
            InputStream inputStream = Channels.newInputStream(input);
            OutputStream outputStream = Channels.newOutputStream(output);
            int length;
            byte[] buffer = new byte[1024 * 4];
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
        }
        output.force(false);
    } finally {
        input.close();
        output.close();
    }
}
 
Example 6
Source File: DataBlockFileHeader.java    From light-task-scheduler with Apache License 2.0 6 votes vote down vote up
@Override
public void write(FileChannel fileChannel) throws IOException {

    byteBuffer().position(0);
    byteBuffer().putShort(magic);
    byteBuffer().putLong(fileLength);
    byteBuffer().putInt(totalNum.get());
    byteBuffer().putInt(aliveNum.get());
    byteBuffer().putInt(isFull);
    byteBuffer().putLong(storeTxLogRecordId);
    byteBuffer().flip();

    fileChannel.position(0);
    fileChannel.write(byteBuffer());
    fileChannel.force(true);
}
 
Example 7
Source File: SplitCsv.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws IOException{
    String src = "F:\\NeembuuUploader\\uploadstats.csv\\uploadstats.n.csv";
    String out = "F:\\NeembuuUploader\\uploadstats.csv\\uploadstats.n.1.csv";
    
    File f = new File(src);
    FileReader fr = new FileReader(f);
    fr.skip(f.length()/2);
    
    BufferedReader br = new BufferedReader(fr);
    
    FileChannel fc_out = FileChannel.open(Paths.get(out),CREATE,WRITE,APPEND);
    FileChannel fc_src = FileChannel.open(Paths.get(src),READ);

    fc_src.position(f.length()/2);
    fc_src.transferTo(f.length()/2, f.length()/2, fc_out);
    fc_out.force(true);
    fc_out.close();
    fc_src.close();
}
 
Example 8
Source File: TranslogWriter.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static TranslogWriter create(Type type, ShardId shardId, String translogUUID, long fileGeneration, Path file, Callback<ChannelReference> onClose, int bufferSize, ChannelFactory channelFactory) throws IOException {
    final BytesRef ref = new BytesRef(translogUUID);
    final int headerLength = getHeaderLength(ref.length);
    final FileChannel channel = channelFactory.open(file);
    try {
        // This OutputStreamDataOutput is intentionally not closed because
        // closing it will close the FileChannel
        final OutputStreamDataOutput out = new OutputStreamDataOutput(java.nio.channels.Channels.newOutputStream(channel));
        CodecUtil.writeHeader(out, TRANSLOG_CODEC, VERSION);
        out.writeInt(ref.length);
        out.writeBytes(ref.bytes, ref.offset, ref.length);
        channel.force(true);
        writeCheckpoint(headerLength, 0, file.getParent(), fileGeneration, StandardOpenOption.WRITE);
        final TranslogWriter writer = type.create(shardId, fileGeneration, new ChannelReference(file, fileGeneration, channel, onClose), bufferSize);
        return writer;
    } catch (Throwable throwable){
        // if we fail to bake the file-generation into the checkpoint we stick with the file and once we recover and that
        // file exists we remove it. We only apply this logic to the checkpoint.generation+1 any other file with a higher generation is an error condition
        IOUtils.closeWhileHandlingException(channel);
        throw throwable;
    }
}
 
Example 9
Source File: Utils.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
public void forceWrite(final String contents, final File file) throws IOException {
    try (FileOutputStream s = new FileOutputStream(file.getAbsolutePath(), false)) {
        s.write(contents.getBytes(StandardCharsets.UTF_8.name()));
        FileChannel c = s.getChannel();
        c.force(true);
        s.getFD().sync();
        c.close();
        s.close();
    }
}
 
Example 10
Source File: FileStore.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Call fsync. Depending on the operating system and hardware, this may or
 * may not in fact write the changes.
 */
public void sync() {
    try {
        for (FileChannel file : this.files) {
            file.force(true);
        }
    } catch (IOException e) {
        closeFileSilently();
        throw TmpFileException.get(ErrorCode.ER_FILE_SYNC, e, name);
    }
}
 
Example 11
Source File: ChatRoom.java    From jReto with MIT License 5 votes vote down vote up
/** Closes the file channel and resets progress. */
private void endTransfer(FileChannel fileChannel) {
	setFileProgress(0, 1);
	try {
		fileChannel.force(true);
		fileChannel.close();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 12
Source File: StoreTxLogFileHeader.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
@Override
public void write(FileChannel fileChannel) throws IOException {
    byteBuffer().position(0);
    byteBuffer().putShort(magic);
    byteBuffer().putLong(this.firstRecordId);
    byteBuffer().flip();

    fileChannel.position(0);
    fileChannel.write(byteBuffer());
    fileChannel.force(true);
}
 
Example 13
Source File: MappedByteBufferQueue.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void flush(T entry) throws IOException {
    Queue<T> inMemQueue = getInMemoryQueue();
    int resultSize = sizeOf(entry);
    maxResultSize = Math.max(maxResultSize, resultSize);
    totalResultSize = hasMaxQueueSize ? maxResultSize * inMemQueue.size() : (totalResultSize + resultSize);
    if (totalResultSize >= thresholdBytes) {
        this.file = File.createTempFile(UUID.randomUUID().toString(), null);
        RandomAccessFile af = new RandomAccessFile(file, "rw");
        FileChannel fc = af.getChannel();
        int writeIndex = 0;
        mappingSize = Math.min(Math.max(maxResultSize, DEFAULT_MAPPING_SIZE), totalResultSize);
        MappedByteBuffer writeBuffer = fc.map(MapMode.READ_WRITE, writeIndex, mappingSize);

        int resSize = inMemQueue.size();
        for (int i = 0; i < resSize; i++) {                
            T e = inMemQueue.poll();
            writeToBuffer(writeBuffer, e);
            // buffer close to exhausted, re-map.
            if (mappingSize - writeBuffer.position() < maxResultSize) {
                writeIndex += writeBuffer.position();
                writeBuffer = fc.map(MapMode.READ_WRITE, writeIndex, mappingSize);
            }
        }
        writeBuffer.putInt(EOF); // end
        fc.force(true);
        fc.close();
        af.close();
        flushedCount = resSize;
        inMemQueue.clear();
        flushBuffer = true;
    }
}
 
Example 14
Source File: AbstractDiskHttpData.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public void setContent(InputStream inputStream) throws IOException {
    if (inputStream == null) {
        throw new NullPointerException("inputStream");
    }
    if (file != null) {
        delete();
    }
    file = tempFile();
    FileOutputStream outputStream = new FileOutputStream(file);
    int written = 0;
    try {
        FileChannel localfileChannel = outputStream.getChannel();
        byte[] bytes = new byte[4096 * 4];
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        int read = inputStream.read(bytes);
        while (read > 0) {
            byteBuffer.position(read).flip();
            written += localfileChannel.write(byteBuffer);
            read = inputStream.read(bytes);
        }
        localfileChannel.force(false);
    } finally {
        outputStream.close();
    }
    size = written;
    if (definedSize > 0 && definedSize < size) {
        file.delete();
        file = null;
        throw new IOException("Out of size: " + size + " > " + definedSize);
    }
    isRenamed = true;
    completed = true;
}
 
Example 15
Source File: CacheUtils.java    From CrawlerForReader with Apache License 2.0 5 votes vote down vote up
private static void writeFileFromBytes(final File file, final byte[] bytes) {
    FileChannel fc = null;
    try {
        fc = new FileOutputStream(file, false).getChannel();
        fc.write(ByteBuffer.wrap(bytes));
        fc.force(true);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        CloseUtils.closeIO(fc);
    }
}
 
Example 16
Source File: DataConsistentFileOutputStream.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static void truncateFileToConsistentSize(FileOutputStream fos, long size) {
    try {
        FileChannel fch = fos.getChannel();
        fch.truncate(size);
        fch.force(true);
    } catch (IOException ex) {
        Logger.getLogger(DataConsistentFileOutputStream.class.getName()).log(
                Level.INFO,
                "Not able to truncate file to the data consistent size of "+size+" bytes.",
                ex);
    }
}
 
Example 17
Source File: FileUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public static void writeFileAsCharset(File file, String contents,
									  String newCharset) throws IOException {
	Log.d("Writing file", file.getAbsolutePath());
	//		System.out.println(contents);
	if (!file.getParentFile().exists()) {
		file.getParentFile().mkdirs();
	}
	File tempF = new File(file.getAbsolutePath() + ".tmp");
	FileOutputStream fos = new FileOutputStream(tempF);
	FileChannel fileChannel = fos.getChannel();
	byte[] oldCharArr = contents.getBytes(newCharset);
	if ("UTF-8".equalsIgnoreCase(newCharset) && 
		!(oldCharArr[0] == -17 && oldCharArr[1] == -69 && oldCharArr[2] == -65
		|| oldCharArr[0] == -61 && oldCharArr[1] == -96 && oldCharArr[2] == 13
		|| oldCharArr[0] == 49 && oldCharArr[1] == 48 && oldCharArr[2] == 41
		|| oldCharArr[0] == 77 && oldCharArr[1] == -31 && oldCharArr[2] == -69)
		) {
		fileChannel.write(ByteBuffer.wrap(UTF8_ESCAPE));
	}
	fileChannel.write(ByteBuffer.wrap(oldCharArr));
	fileChannel.force(true);
	close(fileChannel);
	flushClose(fos);
	file.delete();
	tempF.renameTo(file);
	//boolean renameRet = tempF.renameTo(file);
	//		if (renameRet) {
	//			Log.d("writeFileAsCharset", "rename " + " to " + file + " successfully");
	//		} else {
	//			Log.d("writeFileAsCharset", "rename " + " to " + file + " unsuccessfully");
	//		}
}
 
Example 18
Source File: Files.java    From util with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static File writeDataToTempFileOrDie(@Nonnull final OutputStreamCallback callback,
                                             @Nonnull final File targetFile,
                                             @Nonnull final Logger log) throws IOException {
    Preconditions.checkNotNull(callback, "callback argument is required!");
    Preconditions.checkNotNull(log, "log argument is required!");
    Preconditions.checkNotNull(targetFile, "targetFile argument is required!");

    FileOutputStream fileOut = null;
    FileChannel fileChannel = null;
    try {
        final String targetFinalName = targetFile.getName();
        final File targetDirectory = targetFile.getParentFile();

        // open temporary file
        final File tmpFile = File.createTempFile(targetFinalName, ".tmp", targetDirectory);

        fileOut = new FileOutputStream(tmpFile);
        fileChannel = fileOut.getChannel();

        // make sure to use an output stream that flows THROUGH the FileChannel, so that FileChannel.force(true)
        // can do what it's supposed to

        // write the data AND flush it
        callback.writeAndFlushData(Channels.newOutputStream(fileChannel));

        return tmpFile;
    } finally {
        try {
            // fsync to disk (both data AND length)
            if (fileChannel != null) {
                fileChannel.force(true);
            }
        } finally {
            // close the open file (if during an EXC,
            if (fileOut != null) {
                fileOut.close();
            }
        }
    }
}
 
Example 19
Source File: CacheWriter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void write (BufferedImage img, boolean append, String id) throws IOException {
    File out = getOutfile(append);
    File meta = getMetafile(append);
    System.err.println("Writing to " + out + " and " + meta);
    
    int width = img.getWidth();
    int height = img.getHeight();
    
    ByteBuffer buf = ByteBuffer.allocate(width * height * 4);
    IntBuffer ibuf = buf.asIntBuffer();
  
    for (int y=0; y < height; y++) {
        for (int x=0; x < width; x++) {
            int pixel = img.getRGB(x, y);
            ibuf.put(pixel);
        }
    }
    FileOutputStream fileOut = new FileOutputStream (out, append);
    FileOutputStream metaOut = new FileOutputStream (meta, append);
    FileChannel fileChannel = fileOut.getChannel();
    
    if (append) {
        fileChannel.position(out.length());
    }
    
    //Check the size of the file we're creating - nio bytebuffers are
    //limited to dealing with files < Integer.MAX_VALUE large
    if (fileChannel.position() + buf.limit() > Integer.MAX_VALUE) {
        //Can handle this and create a second cache file in the unlikely
        //event this comes to pass
        throw new BufferOverflowException();
    }
    
    long start = fileChannel.position();
    
    fileChannel.write(buf);
    
    long end = fileChannel.position();
    
    fileChannel.force(true);
    fileChannel.close();
    
    FileChannel metaChannel = metaOut.getChannel();
    if (append) {
        metaChannel.position(meta.length());
    }
    
    metaChannel.write(getMetadata(img, id, start, end));
    metaChannel.force(true);
    metaChannel.close();
}
 
Example 20
Source File: Files.java    From joyqueue with Apache License 2.0 3 votes vote down vote up
/**
 * 从通道里面拷贝数据
 *
 * @param in     输入通道
 * @param out    输出通道
 * @param start  输入位置
 * @param length 长度
 * @throws IOException
 */
protected static void copy(final FileChannel in, final FileChannel out, final long start, final long length) throws
        IOException {
    if (in == null || out == null) {
        return;
    }
    in.transferTo(start, length, out);
    out.force(false);
}