package org.rapidoid.util; import org.rapidoid.RapidoidThing; import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Since; import java.nio.ByteBuffer; /* * #%L * rapidoid-commons * %% * Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ @Authors("Nikolche Mihajlovski") @Since("2.0.0") public class Bufs extends RapidoidThing { public static ByteBuffer expand(ByteBuffer buf, int newSize) { ByteBuffer buf2 = ByteBuffer.allocate(newSize); ByteBuffer buff = buf.duplicate(); buff.rewind(); buff.limit(buff.capacity()); buf2.put(buff); return buf2; } public static ByteBuffer expand(ByteBuffer buf) { int cap = buf.capacity(); if (cap <= 1000) { cap *= 10; } else if (cap <= 10000) { cap *= 5; } else { cap *= 2; } return expand(buf, cap); } public static String buf2str(ByteBuffer buf) { return new String(buf2bytes(buf)); } public static byte[] buf2bytes(ByteBuffer buf) { ByteBuffer buf2 = buf.duplicate(); buf2.rewind(); buf2.limit(buf2.capacity()); byte[] bytes = new byte[buf2.capacity()]; buf2.get(bytes); return bytes; } public static int[] buf2ubytes(ByteBuffer buf) { byte[] bytes = buf2bytes(buf); int[] ubytes = new int[bytes.length]; for (int i = 0; i < bytes.length; i++) { ubytes[i] = Msc.ubyte(bytes[i]); } return ubytes; } public static ByteBuffer buf(String s) { byte[] bytes = s.getBytes(); ByteBuffer buf = ByteBuffer.allocateDirect(bytes.length); buf.put(bytes); buf.rewind(); return buf; } }