Example usage for java.nio MappedByteBuffer clear

List of usage examples for java.nio MappedByteBuffer clear

Introduction

In this page you can find the example usage for java.nio MappedByteBuffer clear.

Prototype

@Override
public final MappedByteBuffer clear() 

Source Link

Usage

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("test.txt", "r");
    FileChannel fc = raf.getChannel();
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    buffer.clear();
    buffer.flip();// w ww. ja  v  a 2  s .c o m

    System.out.println("hasArray=" + buffer.hasArray());
    System.out.println(buffer.toString());

    System.out.flush();
}

From source file:Main.java

public static void cleanMappedByteBuffer(MappedByteBuffer mapping) {
    mapping.clear();
}

From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java

public static void mappedTest(File source, File target) throws Exception {
    FileInputStream fis = null;/*  w  w w .java2 s. c o  m*/
    FileOutputStream fos = null;
    MappedByteBuffer mapbuffer = null;

    try {
        long fileSize = source.length();
        final byte[] outputData = new byte[(int) fileSize];
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);
        FileChannel sChannel = fis.getChannel();

        target.createNewFile();

        mapbuffer = sChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
        for (int i = 0; i < fileSize; i++) {
            outputData[i] = mapbuffer.get();
        }

        mapbuffer.clear();
        fos.write(outputData);
        fos.flush();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);

        if (mapbuffer == null) {
            return;
        }

        final Object buffer = mapbuffer;

        AccessController.doPrivileged(new PrivilegedAction() {

            public Object run() {
                try {
                    Method clean = buffer.getClass().getMethod("cleaner", new Class[0]);

                    if (clean == null) {
                        return null;
                    }
                    clean.setAccessible(true);
                    sun.misc.Cleaner cleaner = (sun.misc.Cleaner) clean.invoke(buffer, new Object[0]);
                    cleaner.clean();
                } catch (Throwable ex) {
                }

                return null;
            }
        });
    }
}

From source file:org.apache.hadoop.hdfs.hoss.db.FileBlockStore.java

/**
 * map a block(4KB) or a segment(256KB)//from ww  w. java  2 s  . c o  m
 * @param index 
 *          the index of block
 * @param useSegments
 *           whether use segmeng?
 * @return   the mapping Buffer
 */
@SuppressWarnings("unchecked")
public final MappedByteBuffer getMmapForIndex(final int index, boolean useSegments) {
    if (!validState)
        throw new InvalidStateException();
    final int mapIdx = (useSegments ? addressIndexToSegment(index) : index);
    final int mapSize = (useSegments ? segmentSize : blockSize);
    try {
        final Reference<MappedByteBuffer> bref = mmaps.get(mapIdx);
        MappedByteBuffer mbb = null;
        if (bref != null) {
            mbb = bref.get();
        }
        if (mbb == null) { // Create mmap
            final long mapOffset = ((long) mapIdx * mapSize);
            mbb = fileChannel.map(FileChannel.MapMode.READ_WRITE, mapOffset, mapSize);
            // mbb.load();
            mmaps.put(mapIdx, new BufferReference<MappedByteBuffer>(mapIdx, mbb));
        } else {
            mbb.clear();
        }
        if (useSegments) { // slice segment
            final int sliceBegin = (addressIndexToSegmentOffset(index) * blockSize);
            final int sliceEnd = (sliceBegin + blockSize);
            mbb.limit(sliceEnd);
            mbb.position(sliceBegin);
            mbb = (MappedByteBuffer) mbb.slice();
        }
        return mbb;
    } catch (IOException e) {
        LOG.error("IOException in getMmapForIndex(" + index + ")", e);
    }
    return null;
}

From source file:org.mitre.math.linear.BufferRealMatrix.java

/**
 * Create a new matrix with the supplied row and column dimensions.
 *
 * @param rows      the number of rows in the new matrix
 * @param columns   the number of columns in the new matrix
 * @param file      the file to use to store the mapped matrix (<code>null</code> allowed and a tempFile will be created)
 * @throws IllegalArgumentException/*from w  w w.  java 2s  . com*/
 * @throws IOException
 */
public BufferRealMatrix(final int rows, final int columns, File file)
        throws IllegalArgumentException, IOException {
    super(rows, columns);
    this.rows = rows;
    this.columns = columns;

    // number of blocks
    this.blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE;
    this.blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE;

    if (file == null) {
        file = File.createTempFile(TEMP_FILE_PREFIX, null);
        LOG.debug(String.format("Created tempFile '%s'", file.getAbsolutePath()));
    }
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    this.dataFileChannel = raf.getChannel();

    long mbbSize = (long) (Double.SIZE / Byte.SIZE) * (long) rows * (long) columns + BUFFER_HEADER_SIZE;
    LOG.debug(String.format("Matrix size will be %d bytes and %d by %d blocks", mbbSize, this.blockRows,
            this.blockColumns));

    MappedByteBuffer bb = this.dataFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, BUFFER_HEADER_SIZE);
    bb.clear();
    bb.putInt(BLOCK_BYTE_SIZE);
    bb.putInt(rows);
    bb.putInt(columns);
    // note: we don't create the layout like BlockedRealMatrix
    // Is this set to zeros? It would be a pain/slow to init it if it is realy big
}

From source file:org.mitre.math.linear.BufferRealMatrix.java

/**
 * Attempts to load in a previously saved matrix from the given {@link FileChannel}
 *
 * @param fileChannel//w  w w  .  jav a 2  s  .c  o m
 * @return
 * @throws IOException
 */
public static BufferRealMatrix loadMatrix(FileChannel fileChannel) throws IOException {
    MappedByteBuffer bb = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, BUFFER_HEADER_SIZE);
    bb.clear();

    int block_size = bb.getInt();
    assert (block_size == BLOCK_BYTE_SIZE);

    int rows = bb.getInt();
    int columns = bb.getInt();
    LOG.debug(String.format("Found matrix of %dx%d with %d block sizes", rows, columns, block_size));
    return new BufferRealMatrix(fileChannel, rows, columns);
}

From source file:yui.classes.utils.IOUtils.java

/**
 *
 * good for Large Files >2Mb/*from w ww. j  a v a  2  s .c o m*/
 * @param filename
 * @return
 */
private static byte[] largeFileReader(String filename) {
    byte[] bytes = null;
    FileChannel fc = null;
    try {
        fc = new FileInputStream(filename).getChannel();

        MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        int size = byteBuffer.capacity();
        if (size > 0) {
            // Retrieve all bytes in the buffer
            byteBuffer.clear();
            bytes = new byte[size];
            byteBuffer.get(bytes, 0, bytes.length);
        }
        fc.close();
    } catch (FileNotFoundException fnf) {
        System.err.println("" + fnf);
    } catch (IOException io) {
        System.err.println("" + io);
    } finally {
        if (fc != null) {
            try {
                fc.close();
            } catch (IOException ioe) {
                // ignore
            }
        }
    }
    return bytes;
}