Java ByteBuffer Get getTsStartSyncByte(ByteBuffer packet, boolean synced)

Here you can find the source of getTsStartSyncByte(ByteBuffer packet, boolean synced)

Description

Get the index of the sync byte of the first detected start packet.

License

Open Source License

Parameter

Parameter Description
packet This is the ByteBuffer to be processed.
synced <i>true</i> if the current position matches the beginning of a packet and detection does not need to be done.

Return

The index value of the first located packet start byte or -1 if a packet start byte was not found.

Declaration

public static int getTsStartSyncByte(ByteBuffer packet, boolean synced) 

Method Source Code

//package com.java2s;
/*/*  www .  j a  v  a2 s .com*/
 * Copyright 2015 The OpenDCT Authors. All Rights Reserved
 *
 * 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.
 */

import java.nio.ByteBuffer;

public class Main {
    public static final int MTS_SYNC_BYTE = 0x47;
    public static final int MTS_PACKET_LEN = 188;

    /**
     * Get the index of the sync byte of the first detected start packet.
     * <p/>
     * The index is relative to the beginning of the array.
     *
     * @param packet This is the byte array to be processed.
     * @param offset This if the offset of the actual data to be processed.
     * @param length This is the length of data to be processed from the provided offset, it must be
     *               at least 752 bytes (4 188 byte TS packets).
     * @param synced <i>true</i> if the offset matches the beginning of a packet and detection does
     *               not need to be done.
     * @return The index value of the first located packet start byte or -1 if a packet start byte
     *         was not found.
     */
    public static int getTsStartSyncByte(byte packet[], int offset,
            int length, boolean synced) {

        int returnByte = -1;
        int firstSyncByte = offset;

        if (!synced) {
            firstSyncByte = getTsSyncByte(packet, offset, length);
        }

        if (firstSyncByte < 0) {
            return returnByte;
        }

        int limit = offset + length;
        for (int i = firstSyncByte; i < limit; i += MTS_PACKET_LEN) {
            if (i + 1 > length) {
                break;
            }
            int tsPayloadUnitStartIndicator = packet[i + 1] & 0x40;

            if (tsPayloadUnitStartIndicator != 0) {
                returnByte = i;
                break;
            }
        }

        return returnByte;
    }

    /**
     * Get the index of the sync byte of the first detected start packet.
     * <p/>
     * This will search the buffer without actually incrementing the position of the buffer. The
     * index is relative to the beginning of the buffer.
     *
     * @param packet This is the ByteBuffer to be processed.
     * @param synced <i>true</i> if the current position matches the beginning of a packet and
     *               detection does not need to be done.
     * @return The index value of the first located packet start byte or -1 if a packet start byte
     *         was not found.
     */
    public static int getTsStartSyncByte(ByteBuffer packet, boolean synced) {

        int returnByte = -1;
        int firstSyncByte = packet.position();

        if (!synced) {
            firstSyncByte = getTsSyncByte(packet);
        }

        if (firstSyncByte < 0) {
            return returnByte;
        }

        int limit = packet.remaining();
        for (int i = firstSyncByte; i < limit; i += MTS_PACKET_LEN) {
            if (i + 1 > packet.remaining()) {
                break;
            }
            int tsPayloadUnitStartIndicator = packet.get(i + 1) & 0x40;

            if (tsPayloadUnitStartIndicator != 0) {
                returnByte = i;
                break;
            }
        }

        return returnByte;
    }

    /**
     * Returns the index of the first a TS sync byte.
     * <p/>
     * According to the standard finding a valid sync byte is done by locating a byte with the value
     * 0x47 and then checking if the sync byte can be found two more times in the right places. The
     * index is relative to the beginning of the array.
     *
     * @param packet This is the byte array to be processed.
     * @param offset This if the offset of the actual data to be processed.
     * @param length This is the length of data to be processed from the provided offset, it must be
     *               at least 752 bytes (4 188 byte TS packets).
     * @return The index value of the first located sync byte or -1 if a sync byte was not found.
     */
    public static int getTsSyncByte(byte packet[], int offset, int length) {
        int returnValue = -1;

        if (length < 752) {
            return returnValue;
        }

        int limit = offset + length;
        for (int i = offset; i < limit; i++) {
            if (packet[i] == MTS_SYNC_BYTE) {
                if (i + (MTS_PACKET_LEN * 2) < limit) {
                    byte sync1 = packet[i + MTS_PACKET_LEN];
                    byte sync2 = packet[i + (MTS_PACKET_LEN * 2)];

                    if (sync1 == MTS_SYNC_BYTE && sync2 == MTS_SYNC_BYTE) {
                        returnValue = i;
                        break;
                    }
                } else {
                    // If this is not true now, it won't be later either, so we should just return
                    // that we didn't find it.
                    break;
                }
            }
        }

        return returnValue;
    }

    /**
     * Returns the index of the first a TS sync byte.
     * <p/>
     * According to the standard finding a valid sync byte is done by locating a byte with the value
     * 0x47 and then checking if the sync byte can be found two more times in the right places. This
     * will not increment the position of the buffer. The index is relative to the beginning of the
     * buffer.
     *
     * @param packet This is the ByteBuffer to be processed.
     * @return The index value of the first located sync byte or -1 if a sync byte was not found.
     */
    public static int getTsSyncByte(ByteBuffer packet) {
        int returnValue = -1;

        if (packet.remaining() < 752) {
            return returnValue;
        }

        int limit = packet.limit();
        for (int i = packet.position(); i < limit; i++) {
            if (packet.get(i) == MTS_SYNC_BYTE) {
                if (i + (MTS_PACKET_LEN * 2) < limit) {
                    byte sync1 = packet.get(i + MTS_PACKET_LEN);
                    byte sync2 = packet.get(i + (MTS_PACKET_LEN * 2));

                    if (sync1 == MTS_SYNC_BYTE && sync2 == MTS_SYNC_BYTE) {
                        returnValue = i;
                        break;
                    }
                } else {
                    // If this is not true now, it won't be later either, so we should just return
                    // that we didn't find it.
                    break;
                }
            }
        }

        return returnValue;

    }
}

Related

  1. getTableUuid(ByteBuffer rowKey)
  2. getTempByteBuffer()
  3. getTerminatedArray(ByteBuffer buf)
  4. getTriByte(ByteBuffer buf)
  5. getTruncatedInt(ByteBuffer bytes, int numBytes)
  6. getTsSyncByte(ByteBuffer packet)
  7. getUByte(ByteBuffer b)
  8. getUleb128(ByteBuffer buffer)
  9. getUsedBytes(ByteBuffer bb)