2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Diagnostics;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Multimedia
23 internal abstract class MediaBufferBase : IMediaBuffer
25 private readonly IntPtr _dataHandle;
27 public int Length { get; }
29 internal MediaBufferBase(IntPtr dataHandle, int size, bool isReadOnly)
31 Debug.Assert(dataHandle != IntPtr.Zero, "dataHandle is null!");
32 Debug.Assert(size >= 0, "size must not be negative!");
34 _dataHandle = dataHandle;
39 internal MediaBufferBase(IntPtr dataHandle, int size) :
40 this(dataHandle, size, false)
44 internal IntPtr DataHandle => _dataHandle;
46 public bool IsReadOnly { get; }
49 /// Validates the range
51 /// <param name="offset"></param>
52 /// <param name="length"></param>
53 /// <exception cref="ArgumentOutOfRangeException">
54 /// offset + length is greater than <see cref="Length"/>.
56 /// offset or length is less than zero.
58 private void ValidateRange(int offset, int length)
60 if (offset + length > Length)
62 throw new ArgumentOutOfRangeException($"{nameof(offset)}, {nameof(length)}",
63 "offset + length can't be greater than length of the buffer.");
67 throw new ArgumentOutOfRangeException(nameof(length), length,
68 $"Length can't be less than zero.");
72 throw new ArgumentOutOfRangeException(nameof(offset), offset,
73 $"Offset can't be less than zero.");
77 public byte this[int index]
81 ThrowIfBufferIsNotReadable();
83 if (index < 0 || index >= Length)
85 throw new ArgumentOutOfRangeException(nameof(index), index,
86 $"Valid index range is [0, { nameof(Length) }).");
89 return Marshal.ReadByte(DataHandle, index);
93 ThrowIfBufferIsNotWritable();
95 Marshal.WriteByte(DataHandle, index, value);
99 private void ThrowIfBufferIsNotReadable()
101 ValidateBufferReadable();
104 private void ThrowIfBufferIsNotWritable()
108 throw new NotSupportedException("The buffer is read-only.");
111 ValidateBufferWritable();
114 internal abstract void ValidateBufferReadable();
116 internal abstract void ValidateBufferWritable();
118 public void CopyFrom(byte[] source, int startIndex, int length, int offset)
120 ThrowIfBufferIsNotReadable();
124 throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex,
125 "startIndex can't be less than zero.");
127 if (startIndex + length > source.Length)
129 throw new ArgumentOutOfRangeException($"{nameof(startIndex)}, {nameof(length)}",
130 "startIndex + length can't be greater than source.Length.");
133 ValidateRange(offset, length);
135 Marshal.Copy(source, startIndex, IntPtr.Add(_dataHandle, offset), length);
138 public void CopyFrom(byte[] source, int startIndex, int length)
140 CopyFrom(source, startIndex, length, 0);
143 public void CopyTo(byte[] dest, int startIndex, int length, int offset)
145 ThrowIfBufferIsNotWritable();
149 throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex,
150 "startIndex can't be less than zero.");
152 if (startIndex + length > dest.Length)
154 throw new ArgumentOutOfRangeException($"{nameof(startIndex)}, {nameof(length)}",
155 "startIndex + length can't be greater than source.Length.");
158 ValidateRange(offset, length);
160 Marshal.Copy(IntPtr.Add(_dataHandle, offset), dest, startIndex, length);
163 public void CopyTo(byte[] dest, int startIndex, int length)
165 CopyTo(dest, startIndex, length, 0);