// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . // ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· // ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ // ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ // ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ // Magicbane Emulator Project © 2013 - 2022 // www.magicbane.com package engine.util; import java.io.UnsupportedEncodingException; import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; public class ByteBufferUtils { public static String getString(ByteBuffer bb) throws BufferUnderflowException { return getString(bb, false, false); } public static String getString(ByteBuffer bb, boolean switchEndian, boolean small) throws BufferUnderflowException { String out = ""; synchronized (bb) { //This version works with non-latin characters int stringLen; if (small) stringLen = (int)bb.get(); else stringLen = bb.getInt(); if (switchEndian) stringLen = ((Integer.reverseBytes(stringLen)) * 2); else stringLen *= 2; byte[] b = new byte[stringLen]; for (int i=0;i 255) length = 255; //limit for smallString synchronized (bb) { // Write length if (small) bb.put((byte)length); else { if (switchEndian) { bb.putInt(Integer.reverseBytes(length)); } else { bb.putInt(length); } } // Write chars for (int i=0;i= (bb.capacity() * 0.9); } //FIXME: Replace these!!! // public static ByteBuffer resizeByteBuffer(ByteBuffer bb, int multiplyer) { // // ByteBuffer out = ByteBuffer.allocate(bb.capacity() * multiplyer); // // // Copy the data to a temp buf // bb.flip(); // out.put(bb); // // return out; // } // // public static ByteBuffer shrinkByteBuffer(ByteBuffer bb) { // // bb.flip(); // ByteBuffer out = ByteBuffer.allocate(bb.remaining()); // out.put(bb); // return out; // } }