Java Code Examples for java.nio.MappedByteBuffer#clear()

The following examples show how to use java.nio.MappedByteBuffer#clear() . 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: FileIO.java    From util.commons with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 复制文件(内存映射)
 *
 * @param   source
 *          原文件
 *
 * @param   target
 *          目标文件
 *
 * @return  true:成功 / false:失败
 *
 * @throws  IOException
 *          文件读写异常
 */
private static boolean copyByMapped(String source, String target) throws IOException {
    RandomAccessFile readFile = new RandomAccessFile(source, "r");
    RandomAccessFile writeFile = new RandomAccessFile(target, "rw");

    long fileLength = readFile.length();

    MappedByteBuffer in = readFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fileLength);
    MappedByteBuffer out = writeFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, fileLength);

    for (int i = 0; i < fileLength; i++) {
        out.put(in.get());
    }

    readFile.close();
    writeFile.close();
    in.clear();
    out.clear();

    return true;
}
 
Example 2
Source File: SpillMap.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void flushBuffer() throws BufferOverflowException {
    if (pagedIn) {
        MappedByteBuffer buffer;
        // Only flush if page was changed
        if (dirtyPage) {
            Collection<byte[]> values = pageMap.values();
            buffer = spillFile.getPage(pageIndex);
            buffer.clear();
            // number of elements
            buffer.putInt(values.size());
            for (byte[] value : values) {
                // element length
                buffer.putInt(value.length);
                // element
                buffer.put(value, 0, value.length);
            }
        }
        buffer = null;
        // Reset page stats
        pageMap.clear();
        totalResultSize = 0;
    }
    pagedIn = false;
    dirtyPage = false;
}
 
Example 3
Source File: BufferTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testBug53637() throws Exception {
    MappedByteBuffer mapped = (MappedByteBuffer) allocateMapped(1);
    mapped.get();
    mapped.rewind();
    mapped.get();

    mapped.rewind();
    mapped.mark();
    mapped.get();
    mapped.reset();
    mapped.get();

    mapped.rewind();
    mapped.get();
    mapped.clear();
    mapped.get();

    mapped.rewind();
    mapped.get();
    mapped.flip();
    mapped.get();
}
 
Example 4
Source File: SpillMap.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void flushBuffer() throws BufferOverflowException {
    if (pagedIn) {
        MappedByteBuffer buffer;
        // Only flush if page was changed
        if (dirtyPage) {
            Collection<byte[]> values = pageMap.values();
            buffer = spillFile.getPage(pageIndex);
            buffer.clear();
            // number of elements
            buffer.putInt(values.size());
            for (byte[] value : values) {
                // element length
                buffer.putInt(value.length);
                // element
                buffer.put(value, 0, value.length);
            }
        }
        buffer = null;
        // Reset page stats
        pageMap.clear();
        totalResultSize = 0;
    }
    pagedIn = false;
    dirtyPage = false;
}
 
Example 5
Source File: LogOperations.java    From VanetSim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method to read a file
 */
public String readFile(String filePath){		
	byte[] data = null;
		try {
			java.io.FileInputStream f1;
			f1 = new java.io.FileInputStream(filePath);
		    FileChannel fc = f1.getChannel();
		    data = new byte[(int) fc.size()];
		    MappedByteBuffer b = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
		    
		    b.get(data);
		    b.clear();
		    fc.close();
		    f1.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	return new String(data);
}
 
Example 6
Source File: LogOperations.java    From VanetSim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method to read a file. Reads a file and returns the header and the content split in an String array
 */
public String[] readFileAndHeader(String filePath){
	byte[] data = null;
	try {
		java.io.FileInputStream f1;
		f1 = new java.io.FileInputStream(filePath);
	    FileChannel fc = f1.getChannel();
	    data = new byte[(int) fc.size()];
	    MappedByteBuffer b = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
	    
	    b.get(data);
	    b.clear();
	    fc.close();
	    f1.close();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	return new String(data).split(java.util.regex.Pattern.quote("*******************"));
}
 
Example 7
Source File: FileNumberEncrypted.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 加解密
 * 将视频文件的数据流前1000个字节中的每个字节与其下标进行异或运算。
 * 解密时只需将加密过的文件再进行一次异或运算即可。
 * @param strFile 源文件绝对路径
 * @return
 */
private boolean encrypt(String strFile) {
    int len = REVERSE_LENGTH;
    try {
        File f = new File(strFile);
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        long totalLen = raf.length();

        if (totalLen < REVERSE_LENGTH){
            len = (int) totalLen;
        }
        FileChannel channel = raf.getChannel();
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, REVERSE_LENGTH);
        byte tmp;
        for (int i = 0; i < len; ++i) {
            byte rawByte = buffer.get(i);
            tmp = (byte) (rawByte ^ i);
            buffer.put(i, tmp);
        }
        buffer.force();
        buffer.clear();
        channel.close();
        raf.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 8
Source File: FileUtil.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
public static void copyFile(File source, File dest) throws IOException {
    log.debug("Copy from {} to {}", source.getAbsoluteFile(), dest.getAbsoluteFile());
    FileInputStream fi = new FileInputStream(source);
    FileChannel fic = fi.getChannel();
    MappedByteBuffer mbuf = fic.map(FileChannel.MapMode.READ_ONLY, 0, source.length());
    fic.close();
    fi.close();
    fi = null;

    // ensure the destination directory exists
    if (!dest.exists()) {
        String destPath = dest.getPath();
        log.debug("Destination path: {}", destPath);
        String destDir = destPath.substring(0, destPath.lastIndexOf(File.separatorChar));
        log.debug("Destination dir: {}", destDir);
        File dir = new File(destDir);
        if (!dir.exists()) {
            if (dir.mkdirs()) {
                log.debug("Directory created");
            } else {
                log.warn("Directory not created");
            }
        }
        dir = null;
    }

    FileOutputStream fo = new FileOutputStream(dest);
    FileChannel foc = fo.getChannel();
    foc.write(mbuf);
    foc.close();
    fo.close();
    fo = null;

    mbuf.clear();
    mbuf = null;
}