Java源码示例:org.lwjgl.system.MemoryUtil
示例1
public void dispose() {
for (ByteBuffer buf : fontBuffers) {
MemoryUtil.memFree(buf);
}
for (Image image : loadedImages) {
image.dispose();
}
loadedFonts.clear();
fontBuffers.clear();
loadedImages.clear();
currentSheets.clear();
if (this.isModernOpenGL()) {
NanoVGGL3.nvgDelete(nvgContext);
} else {
NanoVGGL2.nvgDelete(nvgContext);
}
}
示例2
public void init(Window window) throws Exception {
this.vg = window.getOptions().antialiasing ? nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES) : nvgCreate(NVG_STENCIL_STROKES);
if (this.vg == NULL) {
throw new Exception("Could not init nanovg");
}
fontBuffer = Utils.ioResourceToByteBuffer("/fonts/OpenSans-Bold.ttf", 150 * 1024);
int font = nvgCreateFontMem(vg, FONT_NAME, fontBuffer, 0);
if (font == -1) {
throw new Exception("Could not add font");
}
colour = NVGColor.create();
posx = MemoryUtil.memAllocDouble(1);
posy = MemoryUtil.memAllocDouble(1);
counter = 0;
}
示例3
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例4
public void init(Window window) throws Exception {
this.vg = window.getOptions().antialiasing ? nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES) : nvgCreate(NVG_STENCIL_STROKES);
if (this.vg == NULL) {
throw new Exception("Could not init nanovg");
}
fontBuffer = Utils.ioResourceToByteBuffer("/fonts/OpenSans-Bold.ttf", 150 * 1024);
int font = nvgCreateFontMem(vg, FONT_NAME, fontBuffer, 0);
if (font == -1) {
throw new Exception("Could not add font");
}
colour = NVGColor.create();
posx = MemoryUtil.memAllocDouble(1);
posy = MemoryUtil.memAllocDouble(1);
counter = 0;
}
示例5
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例6
@Override
public void destroyDirectBuffer(final Buffer buffer) {
final long address = getAddress(buffer);
if (address == -1) {
LOGGER.warning("Not found address of the " + buffer);
return;
}
// disable deallocator
final Deallocator deallocator = DEALLOCATORS.remove(address);
if (deallocator == null) {
LOGGER.warning("Not found a deallocator for address " + address);
return;
}
deallocator.setAddress(null);
MemoryUtil.memFree(buffer);
}
示例7
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例8
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例9
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例10
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例11
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例12
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
try (MemoryStack stack = MemoryStack.stackPush()) {
vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
IntBuffer error = stack.mallocInt(1);
long decoder = stb_vorbis_open_memory(vorbis, error, null);
if (decoder == NULL) {
throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
}
stb_vorbis_get_info(decoder, info);
int channels = info.channels();
int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);
pcm = MemoryUtil.memAllocShort(lengthSamples);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
return pcm;
}
}
示例13
public void init(Window window) throws Exception {
this.vg = window.getOptions().antialiasing ? nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES) : nvgCreate(NVG_STENCIL_STROKES);
if (this.vg == NULL) {
throw new Exception("Could not init nanovg");
}
fontBuffer = Utils.ioResourceToByteBuffer("/fonts/OpenSans-Bold.ttf", 150 * 1024);
int font = nvgCreateFontMem(vg, FONT_NAME, fontBuffer, 0);
if (font == -1) {
throw new Exception("Could not add font");
}
colour = NVGColor.create();
posx = MemoryUtil.memAllocDouble(1);
posy = MemoryUtil.memAllocDouble(1);
counter = 0;
}
示例14
@Override
public void init() {
// Set the error handling code: all GLFW errors will be printed to the system error stream (just like println)
GLFWErrorCallback.createPrint(System.err).set();
glfwSetErrorCallback(errorCallback);
// Initialize GLFW:
if (!glfwInit())
throw new IllegalStateException("GLFW initialization failed");
// Configure the GLFW window
windowID = glfwCreateWindow(
640, 480, // Width and height of the drawing canvas in pixels
"Test", // Title of the window
MemoryUtil.NULL, // Monitor ID to use for fullscreen mode, or NULL to use windowed mode (LWJGL JavaDoc)
MemoryUtil.NULL); // Window to share resources with, or NULL to not share resources (LWJGL JavaDoc)
if (windowID == MemoryUtil.NULL)
throw new IllegalStateException("GLFW window creation failed");
glfwMakeContextCurrent(windowID); // Links the OpenGL context of the window to the current thread (GLFW_NO_CURRENT_CONTEXT error)
glfwSwapInterval(1); // Enable VSync, which effective caps the frame-rate of the application to 60 frames-per-second
glfwShowWindow(windowID);
// If you don't add this line, you'll get the following exception:
// java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
glfwMakeContextCurrent(windowID);
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();
// Set the clear color
glClearColor(0.5f, 0.5f, 0.5f, 1f);
setUpMatrices();
}
示例15
@Override
public long create(Canvas canvas, VKData data) throws AWTException {
MemoryStack stack = MemoryStack.stackGet();
int ptr = stack.getPointer();
JAWTDrawingSurface ds = JAWT_GetDrawingSurface(canvas, awt.GetDrawingSurface());
try {
int lock = JAWT_DrawingSurface_Lock(ds, ds.Lock());
if ((lock & JAWT_LOCK_ERROR) != 0)
throw new AWTException("JAWT_DrawingSurface_Lock() failed");
try {
JAWTDrawingSurfaceInfo dsi = JAWT_DrawingSurface_GetDrawingSurfaceInfo(ds, ds.GetDrawingSurfaceInfo());
try {
JAWTX11DrawingSurfaceInfo dsiX11 = JAWTX11DrawingSurfaceInfo.create(dsi.platformInfo());
long display = dsiX11.display();
long window = dsiX11.drawable();
VkXlibSurfaceCreateInfoKHR sci = VkXlibSurfaceCreateInfoKHR.callocStack(stack)
.sType(VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR)
.dpy(display)
.window(window);
long surfaceAddr = stack.nmalloc(8, 8);
int err = nvkCreateXlibSurfaceKHR(data.instance, sci.address(), 0L, surfaceAddr);
long surface = MemoryUtil.memGetLong(surfaceAddr);
stack.setPointer(ptr);
if (err != VK_SUCCESS) {
throw new AWTException("Calling vkCreateXlibSurfaceKHR failed with error: " + err);
}
return surface;
} finally {
JAWT_DrawingSurface_FreeDrawingSurfaceInfo(dsi, ds.FreeDrawingSurfaceInfo());
}
} finally {
JAWT_DrawingSurface_Unlock(ds, ds.Unlock());
}
} finally {
JAWT_FreeDrawingSurface(ds, awt.FreeDrawingSurface());
}
}
示例16
public void cleanup() {
nvgDelete(vg);
if (posx != null) {
MemoryUtil.memFree(posx);
}
if (posy != null) {
MemoryUtil.memFree(posy);
}
}
示例17
@Override
public void setNativeCursor(JmeCursor jmeCursor) {
if (jmeCursor != null) {
Long glfwCursor = jmeToGlfwCursorMap.get(jmeCursor);
if (glfwCursor == null) {
glfwCursor = createGlfwCursor(jmeCursor);
jmeToGlfwCursorMap.put(jmeCursor, glfwCursor);
}
glfwSetCursor(context.getWindowHandle(), glfwCursor);
} else {
glfwSetCursor(context.getWindowHandle(), MemoryUtil.NULL);
}
}
示例18
/**
* Create a Vulkan instance using LWJGL 3.
*
* @return the VkInstance handle
*/
private static VkInstance createInstance() {
VkApplicationInfo appInfo = VkApplicationInfo.calloc()
.sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
.pApplicationName(memUTF8("AWT Vulkan Demo"))
.pEngineName(memUTF8(""))
.apiVersion(VK_MAKE_VERSION(1, 0, 2));
ByteBuffer VK_KHR_SURFACE_EXTENSION = memUTF8(VK_KHR_SURFACE_EXTENSION_NAME);
ByteBuffer VK_KHR_OS_SURFACE_EXTENSION;
if (Platform.get() == Platform.WINDOWS)
VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
else
VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
PointerBuffer ppEnabledExtensionNames = memAllocPointer(2);
ppEnabledExtensionNames.put(VK_KHR_SURFACE_EXTENSION);
ppEnabledExtensionNames.put(VK_KHR_OS_SURFACE_EXTENSION);
ppEnabledExtensionNames.flip();
VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
.pNext(0L)
.pApplicationInfo(appInfo);
if (ppEnabledExtensionNames.remaining() > 0) {
pCreateInfo.ppEnabledExtensionNames(ppEnabledExtensionNames);
}
PointerBuffer pInstance = MemoryUtil.memAllocPointer(1);
int err = vkCreateInstance(pCreateInfo, null, pInstance);
if (err != VK_SUCCESS) {
throw new RuntimeException("Failed to create VkInstance: " + translateVulkanResult(err));
}
long instance = pInstance.get(0);
memFree(pInstance);
VkInstance ret = new VkInstance(instance, pCreateInfo);
memFree(ppEnabledExtensionNames);
memFree(VK_KHR_OS_SURFACE_EXTENSION);
memFree(VK_KHR_SURFACE_EXTENSION);
appInfo.free();
return ret;
}
示例19
public void cleanup() {
nvgDelete(vg);
if (posx != null) {
MemoryUtil.memFree(posx);
}
if (posy != null) {
MemoryUtil.memFree(posy);
}
}
示例20
@Override
public void cleanUp() {
super.cleanUp();
if (this.instanceDataBuffer != null) {
MemoryUtil.memFree(this.instanceDataBuffer);
this.instanceDataBuffer = null;
}
}
示例21
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) {
super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0));
this.numInstances = numInstances;
glBindVertexArray(vaoId);
instanceDataVBO = glGenBuffers();
vboIdList.add(instanceDataVBO);
instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS);
glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO);
int start = 5;
int strideStart = 0;
// Model matrix
for (int i = 0; i < 4; i++) {
glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart);
glVertexAttribDivisor(start, 1);
glEnableVertexAttribArray(start);
start++;
strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES;
}
// Texture offsets
glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart);
glVertexAttribDivisor(start, 1);
glEnableVertexAttribArray(start);
strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2;
start++;
// Selected or Scaling (for particles)
glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart);
glVertexAttribDivisor(start, 1);
glEnableVertexAttribArray(start);
start++;
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
示例22
@Override
public void cleanUp() {
super.cleanUp();
if (this.instanceDataBuffer != null) {
MemoryUtil.memFree(this.instanceDataBuffer);
this.instanceDataBuffer = null;
}
}
示例23
/**
* Returns the UTF16 string value at the specified byte range.
*/
public String stringValueUTF16(int offset, int limit) {
buffer.position(offset);
buffer.limit(limit);
try {
return MemoryUtil.memUTF16(buffer);
} finally {
buffer.clear();
}
}
示例24
public void cleanup() {
nvgDelete(vg);
if (posx != null) {
MemoryUtil.memFree(posx);
}
if (posy != null) {
MemoryUtil.memFree(posy);
}
}
示例25
/**
* ASCII encodes the specified strings and ensures space for two additional
* buffers filled with the lengths and memory addresses of the encoded
* strings, respectively. The lengths are 4-bytes integers and the memory
* address buffer starts immediately after the lengths buffer.
*
* <p>
* The encoded buffers must be later freed with
* {@link #pointerArrayFree(int, int)}.</p>
*
* @return the offset to the lengths buffer
*/
public int pointerArrayParamASCIIi(CharSequence... strings) {
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
int lengthsAddress = bufferParam(strings.length << 2);
for (int i = 0; i < strings.length; i++) {
ByteBuffer buffer = MemoryUtil.memASCII(strings[i]);
pointerParam(buffersAddress, i, memAddress(buffer));
intParam(lengthsAddress, i, buffer.remaining());
}
return buffersAddress;
}
示例26
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) {
super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0));
this.numInstances = numInstances;
glBindVertexArray(vaoId);
instanceDataVBO = glGenBuffers();
vboIdList.add(instanceDataVBO);
instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS);
glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO);
int start = 5;
int strideStart = 0;
// Model matrix
for (int i = 0; i < 4; i++) {
glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart);
glVertexAttribDivisor(start, 1);
glEnableVertexAttribArray(start);
start++;
strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES;
}
// Texture offsets
glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart);
glVertexAttribDivisor(start, 1);
glEnableVertexAttribArray(start);
strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2;
start++;
// Selected or Scaling (for particles)
glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart);
glVertexAttribDivisor(start, 1);
glEnableVertexAttribArray(start);
start++;
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
示例27
@Override
public void cleanUp() {
super.cleanUp();
if (this.instanceDataBuffer != null) {
MemoryUtil.memFree(this.instanceDataBuffer);
this.instanceDataBuffer = null;
}
}
示例28
public void create() {
vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
FloatBuffer positionBuffer = MemoryUtil.memAllocFloat(vertices.length * 3);
float[] positionData = new float[vertices.length * 3];
for (int i = 0; i < vertices.length; i++) {
positionData[i * 3] = vertices[i].getPosition().getX();
positionData[i * 3 + 1] = vertices[i].getPosition().getY();
positionData[i * 3 + 2] = vertices[i].getPosition().getZ();
}
positionBuffer.put(positionData).flip();
pbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, pbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, positionBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip();
ibo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
示例29
private String Log(long device) {
Utils.pointerBuffers[0].rewind();
int ret = CL10.clGetProgramBuildInfo(program, device, CL10.CL_PROGRAM_BUILD_LOG, (ByteBuffer) null, Utils.pointerBuffers[0]);
Utils.checkError(ret, "clGetProgramBuildInfo");
int count = (int) Utils.pointerBuffers[0].get(0);
final ByteBuffer buffer = BufferUtils.createByteBuffer(count);
ret = CL10.clGetProgramBuildInfo(program, device, CL10.CL_PROGRAM_BUILD_LOG, buffer, null);
Utils.checkError(ret, "clGetProgramBuildInfo");
return MemoryUtil.memASCII(buffer);
}
示例30
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;
}