Java Code Examples for org.lwjgl.system.MemoryUtil#memAlloc()

The following examples show how to use org.lwjgl.system.MemoryUtil#memAlloc() . 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: Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = MemoryUtil.memAlloc((int) fc.size() + 1);
            while (fc.read(buffer) != -1) ;
        }
    } else {
        try (
            InputStream source = Utils.class.getResourceAsStream(resource);
            ReadableByteChannel rbc = Channels.newChannel(source)) {
            buffer = MemoryUtil.memAlloc(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}
 
Example 2
Source File: Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = MemoryUtil.memAlloc((int) fc.size() + 1);
            while (fc.read(buffer) != -1) ;
        }
    } else {
        try (
            InputStream source = Utils.class.getResourceAsStream(resource);
            ReadableByteChannel rbc = Channels.newChannel(source)) {
            buffer = MemoryUtil.memAlloc(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}
 
Example 3
Source File: Utils.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = MemoryUtil.memAlloc((int) fc.size() + 1);
            while (fc.read(buffer) != -1) ;
        }
    } else {
        try (
            InputStream source = Utils.class.getResourceAsStream(resource);
            ReadableByteChannel rbc = Channels.newChannel(source)) {
            buffer = MemoryUtil.memAlloc(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}