Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright 2008 - 2011
 * 
 * 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.
 * 
 * @project loonframework
 * @author chenpeng
 * @emailceponline@yahoo.com.cn
 * @version 0.1
 */

import java.nio.Buffer;
import java.nio.ByteBuffer;

import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

public class Main {
    public static int getOffset(Buffer buf) {
        return buf.position() << getElementSizeExponent(buf);
    }

    public static int getElementSizeExponent(Buffer buf) {
        if (buf instanceof ByteBuffer) {
            return 0;
        } else if (buf instanceof ShortBuffer || buf instanceof CharBuffer) {
            return 1;
        } else if (buf instanceof FloatBuffer || buf instanceof IntBuffer) {
            return 2;
        } else if (buf instanceof LongBuffer || buf instanceof DoubleBuffer) {
            return 3;
        } else {
            throw new IllegalStateException("Unsupported buf type: " + buf);
        }
    }
}