Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

In this page you can find the example usage for java.nio ByteBuffer position.

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

From source file:io.mycat.util.ByteBufferUtil.java

/**
 * You should almost never use this.  Instead, use the write* methods to avoid copies.
 *///  w ww.  j a  v a 2  s .  c  o  m
public static byte[] getArray(ByteBuffer buffer) {
    int length = buffer.remaining();

    if (buffer.hasArray()) {
        int boff = buffer.arrayOffset() + buffer.position();
        return Arrays.copyOfRange(buffer.array(), boff, boff + length);
    }
    // else, DirectByteBuffer.get() is the fastest route
    byte[] bytes = new byte[length];
    buffer.duplicate().get(bytes);

    return bytes;
}

From source file:com.unister.semweb.drums.TestUtils.java

/**
 * Reads from the given numbe of elements (<code>numberOfElementsToRead</code>) from the given file from the
 * beginning.//  w w  w  .  j av a2 s  .com
 */
public static List<DummyKVStorable> readFrom(String filename, int numberOfElementsToRead) throws Exception {
    HeaderIndexFile<DummyKVStorable> file = new HeaderIndexFile<DummyKVStorable>(filename, AccessMode.READ_ONLY,
            1, TestUtils.gp);
    ByteBuffer dataBuffer = ByteBuffer.allocate(numberOfElementsToRead * TestUtils.gp.getElementSize());
    file.read(0, dataBuffer);
    dataBuffer.flip();

    List<DummyKVStorable> readData = new ArrayList<DummyKVStorable>();
    while (dataBuffer.position() < dataBuffer.limit()) {
        byte[] oneLinkData = new byte[TestUtils.gp.getElementSize()];
        dataBuffer.get(oneLinkData);
        DummyKVStorable oneDate = TestUtils.gp.getPrototype().fromByteBuffer(ByteBuffer.wrap(oneLinkData));
        readData.add(oneDate);
    }
    file.close();
    return readData;
}

From source file:com.github.chenxiaolong.dualbootpatcher.RomUtils.java

private static boolean usesLiveWallpaper(RomInformation info, MbtoolInterface iface)
        throws IOException, MbtoolException, MbtoolCommandException {
    String wallpaperInfoPath = info.getDataPath() + "/system/users/0/wallpaper_info.xml";

    int id = -1;/*from w  w w. ja v a  2 s  . c om*/

    try {
        id = iface.fileOpen(wallpaperInfoPath, new short[] { FileOpenFlag.RDONLY }, 0);

        StatBuf sb = iface.fileStat(id);

        // Check file size
        if (sb.st_size < 0 || sb.st_size > 1024) {
            return false;
        }

        // Read file into memory
        byte[] data = new byte[(int) sb.st_size];
        int nWritten = 0;
        while (nWritten < data.length) {
            ByteBuffer newData = iface.fileRead(id, 10240);

            int nRead = newData.limit() - newData.position();
            newData.get(data, nWritten, nRead);
            nWritten += nRead;
        }

        iface.fileClose(id);
        id = -1;

        String xml = new String(data, Charsets.UTF_8);
        return xml.contains("component=");
    } finally {
        if (id >= 0) {
            try {
                iface.fileClose(id);
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}

From source file:com.github.chenxiaolong.dualbootpatcher.RomUtils.java

public static CacheWallpaperResult cacheWallpaper(Context context, RomInformation info, MbtoolInterface iface)
        throws IOException, MbtoolException, MbtoolCommandException {
    if (usesLiveWallpaper(info, iface)) {
        // We can't render a snapshot of a live wallpaper
        return CacheWallpaperResult.USES_LIVE_WALLPAPER;
    }/*from   w w  w. j  av  a2  s  . c  om*/

    String wallpaperPath = info.getDataPath() + "/system/users/0/wallpaper";
    File wallpaperCacheFile = new File(info.getWallpaperPath());
    FileOutputStream fos = null;

    int id = -1;

    try {
        id = iface.fileOpen(wallpaperPath, new short[] {}, 0);

        // Check if we need to re-cache the file
        StatBuf sb = iface.fileStat(id);

        if (wallpaperCacheFile.exists() && wallpaperCacheFile.lastModified() / 1000 > sb.st_mtime) {
            Log.d(TAG, "Wallpaper for " + info.getId() + " has not been changed");
            return CacheWallpaperResult.UP_TO_DATE;
        }

        // Ignore large wallpapers
        if (sb.st_size < 0 || sb.st_size > 20 * 1024 * 1024) {
            return CacheWallpaperResult.FAILED;
        }

        // Read file into memory
        byte[] data = new byte[(int) sb.st_size];
        int nWritten = 0;
        while (nWritten < data.length) {
            ByteBuffer newData = iface.fileRead(id, 10240);

            int nRead = newData.limit() - newData.position();
            newData.get(data, nWritten, nRead);
            nWritten += nRead;
        }

        iface.fileClose(id);
        id = -1;

        fos = new FileOutputStream(wallpaperCacheFile);

        // Compression can be very slow (more than 10 seconds) for a large wallpaper, so we'll
        // just cache the actual file instead
        fos.write(data);

        // Load into bitmap
        //Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        //if (bitmap == null) {
        //    return false;
        //}
        //bitmap.compress(Bitmap.CompressFormat.WEBP, 100, fos);
        //bitmap.recycle();

        // Invalidate picasso cache
        Picasso.with(context).invalidate(wallpaperCacheFile);

        Log.d(TAG, "Wallpaper for " + info.getId() + " has been cached");
        return CacheWallpaperResult.UPDATED;
    } finally {
        if (id >= 0) {
            try {
                iface.fileClose(id);
            } catch (IOException e) {
                // Ignore
            }
        }
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static boolean compact(ByteBuffer buffer) {
    if (buffer.position() == 0) {
        return false;
    }//from ww  w  .j  a va 2  s . c  om
    boolean full = buffer.limit() == buffer.capacity();
    buffer.compact().flip();
    return full && buffer.limit() < buffer.capacity();
}

From source file:Main.java

/**
 * Returns new byte buffer whose content is a shared subsequence of this buffer's content
 * between the specified start (inclusive) and end (exclusive) positions. As opposed to
 * {@link ByteBuffer#slice()}, the returned buffer's byte order is the same as the source
 * buffer's byte order.//from  w  w w.  j  a  v  a2  s  . c  o  m
 */
private static ByteBuffer sliceFromTo(final ByteBuffer source, final int start, final int end) {
    if (start < 0) {
        throw new IllegalArgumentException("start: " + start);
    }
    if (end < start) {
        throw new IllegalArgumentException("end < start: " + end + " < " + start);
    }
    final int capacity = source.capacity();
    if (end > source.capacity()) {
        throw new IllegalArgumentException("end > capacity: " + end + " > " + capacity);
    }
    final int originalLimit = source.limit();
    final int originalPosition = source.position();
    try {
        source.position(0);
        source.limit(end);
        source.position(start);
        final ByteBuffer result = source.slice();
        result.order(source.order());
        return result;
    } finally {
        source.position(0);
        source.limit(originalLimit);
        source.position(originalPosition);
    }
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * Convert a byte buffer to an integer. Does not change the byte buffer
 * position./*w  w w  .ja v a 2  s.c o  m*/
 * 
 * @param bytes
 *            byte buffer to convert to integer
 * @return int representation of the byte buffer
 */
public static int toInt(ByteBuffer bytes) {
    return bytes.getInt(bytes.position());
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static long toLong(ByteBuffer bytes) {
    return bytes.getLong(bytes.position());
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static float toFloat(ByteBuffer bytes) {
    return bytes.getFloat(bytes.position());
}

From source file:com.glaf.core.util.ByteBufferUtils.java

public static double toDouble(ByteBuffer bytes) {
    return bytes.getDouble(bytes.position());
}