Here you can find the source of decodeAlign(ByteBuffer buf)
static void decodeAlign(ByteBuffer buf)
//package com.java2s; /**// w ww . j a va 2s.co m * Copyright 2010 Mark Aylett <mark.aylett@gmail.com> * * The contents of this file are subject to the Common Development and * Distribution License (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.sun.com/cddl/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for * the specific language governing rights and limitations under the License. */ import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; public class Main { private static final int[] ALIGN = { 0, 3, 2, 1 }; static void decodeAlign(ByteBuffer buf) { final int pos = buf.position(); final int newPos = aligned(pos); if (buf.limit() < newPos) throw new BufferUnderflowException(); buf.position(newPos); } static void decodeAlign(ByteBuffer buf, byte[] val, int offset, int len) { final int pos = buf.position(); if (buf.hasArray()) { final int newPos = aligned(pos + len); if (buf.limit() < newPos) throw new BufferUnderflowException(); System.arraycopy(buf.array(), pos, val, offset, len); buf.position(newPos); // Aligned. } else { buf.get(val, offset, len); decodeAlign(buf); } } static int aligned(int n) { return n + ALIGN[n % 4]; } }