Java ByteBuffer Get getTsSyncByte(ByteBuffer packet)

Here you can find the source of getTsSyncByte(ByteBuffer packet)

Description

Returns the index of the first a TS sync byte.

License

Open Source License

Parameter

Parameter Description
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.

Declaration

public static int getTsSyncByte(ByteBuffer packet) 

Method Source Code

//package com.java2s;
/*/*from  w w  w  .j a va  2s.  co m*/
 * 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;

    /**
     * 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. getTempByteBuffer()
  2. getTerminatedArray(ByteBuffer buf)
  3. getTriByte(ByteBuffer buf)
  4. getTruncatedInt(ByteBuffer bytes, int numBytes)
  5. getTsStartSyncByte(ByteBuffer packet, boolean synced)
  6. getUByte(ByteBuffer b)
  7. getUleb128(ByteBuffer buffer)
  8. getUsedBytes(ByteBuffer bb)
  9. getUTF8FromByteBuffer(ByteBuffer bb)