List of usage examples for java.nio ByteBuffer capacity
public final int capacity()
From source file:org.skife.muckery.mappy.ByteUtils.java
/** * If we have no more room in the current buffer, then double our capacity * and copy the current buffer to the new one. * <p/>/*from w w w . j a va2 s. c om*/ * Note: the new buffer is allocated using ByteBuffer.allocate. * * @param buffer The buffer from which to copy the contents * @param newCapacity The new capacity size */ public static ByteBuffer expand(ByteBuffer buffer, int newCapacity) { if (newCapacity < buffer.capacity()) throw new IllegalArgumentException("newCapacity (" + newCapacity + ") must be larger than existing capacity (" + buffer.capacity() + ")"); ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity); int position = buffer.position(); buffer.rewind(); newBuffer.put(buffer); newBuffer.position(position); return newBuffer; }
From source file:org.bimserver.webservices.authorization.Authorization.java
public static Authorization fromToken(SecretKeySpec key, String token) throws AuthenticationException { if (token == null) { throw new IllegalArgumentException("Token required"); }/* w w w.ja v a 2 s . c o m*/ try { int hashSizeBytes = 16; Cipher decodingCipher = Cipher.getInstance("AES"); decodingCipher.init(Cipher.DECRYPT_MODE, key); ByteBuffer buffer = ByteBuffer.wrap(decodingCipher.doFinal(Hex.decodeHex(token.toCharArray()))); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] foundHash = new byte[hashSizeBytes]; buffer.get(foundHash, 0, hashSizeBytes); byte[] hashInput = new byte[buffer.capacity() - hashSizeBytes]; buffer.get(hashInput); buffer.position(hashSizeBytes); byte[] calculatedHash = messageDigest.digest(hashInput); if (Arrays.equals(foundHash, calculatedHash)) { byte type = buffer.get(); Authorization authorization = null; long expires = buffer.getLong(); long uoid = buffer.getLong(); switch (type) { case ExplicitRightsAuthorization.ID: authorization = ExplicitRightsAuthorization.fromBuffer(buffer); break; case UserAuthorization.ID: authorization = UserAuthorization.fromBuffer(buffer); break; case SystemAuthorization.ID: authorization = SystemAuthorization.fromBuffer(buffer); break; case AnonymousAuthorization.ID: authorization = AnonymousAuthorization.fromBuffer(buffer); break; case AdminAuthorization.ID: authorization = AdminAuthorization.fromBuffer(buffer); break; case SingleProjectAuthorization.ID: authorization = SingleProjectAuthorization.fromBuffer(buffer); break; default: throw new AuthenticationException("Unknown authorization type: " + type); } authorization.setUoid(uoid); authorization.setExpires(expires); if (authorization.getExpires().getTimeInMillis() < new GregorianCalendar().getTimeInMillis()) { throw new AuthenticationException("This token has expired"); } return authorization; } else { throw new AuthenticationException("Given token is corrupt"); } } catch (GeneralSecurityException e) { throw new AuthenticationException("Invalid token", e); } catch (DecoderException e) { throw new AuthenticationException(e); } }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static boolean isFull(ByteBuffer buff) { return buff != null && buff.limit() == buff.capacity(); }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static int space(ByteBuffer buffer) { if (buffer == null) { return 0; }/*from w ww. j a va 2 s . com*/ return buffer.capacity() - buffer.limit(); }
From source file:org.jtrfp.trcl.core.Texture.java
public static ByteBuffer fragmentRGBA(ByteBuffer input, int quadDepth, int x, int y) { final int originalSideLen = (int) Math.sqrt(input.capacity() / 4); final int splitAmount = (int) Math.pow(2, quadDepth); final int newSideLen = originalSideLen / splitAmount; ByteBuffer result = ByteBuffer.allocateDirect((int) (Math.pow(newSideLen, 2) * 4)); for (int row = y * newSideLen; row < (y + 1) * newSideLen; row++) { input.clear();//from w ww . j ava 2 s .com input.limit((x + 1) * newSideLen * 4 + row * originalSideLen * 4); input.position(x * newSideLen * 4 + row * originalSideLen * 4); result.put(input); } return result; }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * buffer, fill?//from w w w. ja v a 2s . c o m * * @param buffer */ public static void clearToFill(ByteBuffer buffer) { if (buffer != null) { buffer.position(0); buffer.limit(buffer.capacity()); } }
From source file:org.jtrfp.trcl.core.Texture.java
public static ByteBuffer indexed2RGBA8888(ByteBuffer indexedPixels, Color[] palette) { Color color;//from w ww . j a va 2 s. c o m ByteBuffer buf = ByteBuffer.allocateDirect(indexedPixels.capacity() * 4); final int cap = indexedPixels.capacity(); for (int i = 0; i < cap; i++) { color = palette[(indexedPixels.get() & 0xFF)]; buf.put((byte) color.getRed()); buf.put((byte) color.getGreen()); buf.put((byte) color.getBlue()); buf.put((byte) color.getAlpha()); } // end for(i) buf.clear();// Rewind return buf; }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static boolean compact(ByteBuffer buffer) { if (buffer.position() == 0) { return false; }/*w w w .j a v a 2s . c o m*/ boolean full = buffer.limit() == buffer.capacity(); buffer.compact().flip(); return full && buffer.limit() < buffer.capacity(); }
From source file:com.gamesalutes.utils.ByteUtils.java
/** * Extends the size of <code>buf</code> to at least meet <code>minCap</code>. * If <code>buf</code> is too small, then a new buffer is allocated and * any existing contents in <code>buf</code> will be transfered. The position * of the new buffer will be that of the old buffer if it was not <code>null</code>, and * the previous mark will be discarded if one was set. * //from w w w. j av a 2 s .co m * @param buf the input <code>ByteBuffer</code> * @param minCap the minimum capacity * @return a <code>ByteBuffer</code> that can meet <code>minCap</code> */ public static ByteBuffer growBuffer(ByteBuffer buf, int minCap) { int myLimit = buf != null ? buf.limit() : 0; // limit can accomidate capacity requirements if (buf != null && myLimit >= minCap) return buf; int myCap = buf != null ? buf.capacity() : 0; // capacity can accomidate but limit is too small if (buf != null && myCap >= minCap) { buf.limit(myCap); return buf; } else //if(myCap < minCap) { ByteBuffer newBuffer = null; if (myCap == 0) myCap = 1; while (myCap < minCap) myCap <<= 1; if (buf != null && buf.isDirect()) newBuffer = ByteBuffer.allocateDirect(myCap); else newBuffer = ByteBuffer.allocate(myCap); // copy contents of original buffer if (buf != null) { int pos = buf.position(); buf.clear(); newBuffer.put(buf); newBuffer.position(pos); } return newBuffer; } }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * fill?//from www.ja va2 s . c om * * @param buffer * @return */ public static int flipToFill(ByteBuffer buffer) { int position = buffer.position(); int limit = buffer.limit(); // flush??fill? if (position == limit) { buffer.position(0); buffer.limit(buffer.capacity()); return 0; } // ?limit equal capacity,? int capacity = buffer.capacity(); if (limit == capacity) { buffer.compact(); return 0; } // ?? buffer.position(limit); buffer.limit(capacity); return position; }