Java ByteBuffer Decode decodeAlign(ByteBuffer buf)

Here you can find the source of decodeAlign(ByteBuffer buf)

Description

decode Align

License

CDDL license

Declaration

static void decodeAlign(ByteBuffer buf) 

Method Source Code

//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];
    }
}

Related

  1. decode(ByteBuffer bb)
  2. decode(ByteBuffer bb, CharsetDecoder decoder)
  3. decode(Charset charset, ByteBuffer buffer)
  4. decode(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst)
  5. decode(String charsetName, ByteBuffer byteBuffer)
  6. decodeASCII(ByteBuffer buffer, int length, int offset)
  7. decodeBuffer(ByteBuffer buffer, String charsetName)
  8. decodeDouble(ByteBuffer bb)
  9. decodeInt(ByteBuffer buffer, int start)