001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.meta.block;
018
019import javax.annotation.concurrent.ThreadSafe;
020
021/**
022 * Utility class for manipulating byte arrays
023 *
024 * @author The TridentSDK Team
025 * @since 0.4-alpha
026 */
027@ThreadSafe
028public final class ByteArray {
029    private ByteArray() {
030    }
031
032    /**
033     * Writes the second end of the short to a byte array
034     *
035     * @param s the short to write
036     * @return the encoded short
037     */
038    public static byte writeSecond(short s) {
039        return (byte) s;
040    }
041
042    /**
043     * Writes the first end of the short of the byte array
044     *
045     * @param s the short to write
046     * @return the encoded short
047     */
048    public static byte writeFirst(short s) {
049        return (byte) ((s >> 8) & 0xFF);
050    }
051
052    /**
053     * Reads a short from two bytes in big endian order
054     *
055     * @param first  the first byte
056     * @param second the second byte
057     * @return the decoded short
058     */
059    public static short readShort(byte first, byte second) {
060        return (short) ((first << 8) + (second & 0xFF));
061    }
062}