1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.exoplayer2.decoder; 17 18 import com.google.android.exoplayer2.C; 19 20 /** 21 * Base class for buffers with flags. 22 */ 23 public abstract class Buffer { 24 25 @C.BufferFlags 26 private int flags; 27 28 /** 29 * Clears the buffer. 30 */ clear()31 public void clear() { 32 flags = 0; 33 } 34 35 /** 36 * Returns whether the {@link C#BUFFER_FLAG_DECODE_ONLY} flag is set. 37 */ isDecodeOnly()38 public final boolean isDecodeOnly() { 39 return getFlag(C.BUFFER_FLAG_DECODE_ONLY); 40 } 41 42 /** 43 * Returns whether the {@link C#BUFFER_FLAG_END_OF_STREAM} flag is set. 44 */ isEndOfStream()45 public final boolean isEndOfStream() { 46 return getFlag(C.BUFFER_FLAG_END_OF_STREAM); 47 } 48 49 /** 50 * Returns whether the {@link C#BUFFER_FLAG_KEY_FRAME} flag is set. 51 */ isKeyFrame()52 public final boolean isKeyFrame() { 53 return getFlag(C.BUFFER_FLAG_KEY_FRAME); 54 } 55 56 /** Returns whether the {@link C#BUFFER_FLAG_HAS_SUPPLEMENTAL_DATA} flag is set. */ hasSupplementalData()57 public final boolean hasSupplementalData() { 58 return getFlag(C.BUFFER_FLAG_HAS_SUPPLEMENTAL_DATA); 59 } 60 61 /** 62 * Replaces this buffer's flags with {@code flags}. 63 * 64 * @param flags The flags to set, which should be a combination of the {@code C.BUFFER_FLAG_*} 65 * constants. 66 */ setFlags(@.BufferFlags int flags)67 public final void setFlags(@C.BufferFlags int flags) { 68 this.flags = flags; 69 } 70 71 /** 72 * Adds the {@code flag} to this buffer's flags. 73 * 74 * @param flag The flag to add to this buffer's flags, which should be one of the 75 * {@code C.BUFFER_FLAG_*} constants. 76 */ addFlag(@.BufferFlags int flag)77 public final void addFlag(@C.BufferFlags int flag) { 78 flags |= flag; 79 } 80 81 /** 82 * Removes the {@code flag} from this buffer's flags, if it is set. 83 * 84 * @param flag The flag to remove. 85 */ clearFlag(@.BufferFlags int flag)86 public final void clearFlag(@C.BufferFlags int flag) { 87 flags &= ~flag; 88 } 89 90 /** 91 * Returns whether the specified flag has been set on this buffer. 92 * 93 * @param flag The flag to check. 94 * @return Whether the flag is set. 95 */ getFlag(@.BufferFlags int flag)96 protected final boolean getFlag(@C.BufferFlags int flag) { 97 return (flags & flag) == flag; 98 } 99 100 } 101