/// <summary> /// Tries to evaluate byte count prefix of a blob and evaluate it. /// </summary> static bool TryReadAndEvaluateByteCountPrefix(byte[] data, int index, out int byteCountPrefixValue) { FourByteOscData byteCountPrefix; if (!FourByteOscData.TryReadFrom(data, ref index, out byteCountPrefix)) { // Not enough space for byte count prefix in data array. byteCountPrefixValue = 0; if (OscGlobals.logWarnings) { Debug.LogWarning(string.Format("{0} Blob is missing byte count prefix.\n", logPrepend)); } return(false); } int multipleOfFourByteCount = ((byteCountPrefix.intValue + 3) / 4) * 4; // No trailing null, rounded to 4 bytes. if (index + multipleOfFourByteCount > data.Length) { // Not enough space for blob in data array. byteCountPrefixValue = 0; if (OscGlobals.logWarnings) { Debug.LogWarning(string.Format("{0} Blob data is incomplete.\n", logPrepend)); } return(false); } byteCountPrefixValue = byteCountPrefix.intValue; return(true); }
/// <summary> /// Tries to evaluate byte count prefix of a blob and evaluate it. /// </summary> // TODO When .Net 4.5 becomes available in Unity: Replace IList<T> with IReadOnlyList<T> since we want to pass Array where number and order of list elements is read-only. public static bool TryReadAndEvaluateByteCountPrefix(IList <byte> data, int index, out int byteCountPrefixValue) { FourByteOscData byteCountPrefix; if (!FourByteOscData.TryReadFrom(data, ref index, out byteCountPrefix)) { // Not enough space for byte count prefix in data array. byteCountPrefixValue = 0; StringBuilder sb = OscDebug.BuildText(); sb.Append("Blob is missing byte count prefix.\n"); Debug.LogWarning(sb.ToString()); return(false); } int multipleOfFourByteCount = ((byteCountPrefix.intValue + 3) / 4) * 4; // Multiple of four bytes. if (index + multipleOfFourByteCount > data.Count) { // Not enough space for blob in data array. byteCountPrefixValue = 0; StringBuilder sb = OscDebug.BuildText(); sb.Append("Blob data is incomplete.\n"); Debug.LogWarning(sb.ToString()); return(false); } byteCountPrefixValue = byteCountPrefix.intValue; return(true); }
public static bool TryReadFrom(byte[] data, ref int index, out FourByteOscData value) { if (index + byteCount > data.Length) { value = new FourByteOscData(); return(false); } value = new FourByteOscData(data[index++], data[index++], data[index++], data[index++]); return(true); }
public static int AddTo(byte[] blob, List <byte> data) { // First 4 bytes hold the byte blob length. FourByteOscData byteCount = new FourByteOscData(blob.Length); byteCount.AddTo(data); // Then the blob. data.AddRange(blob); // Then enough zero bytes to make the blob multiple of four bytes. int trailingZeroCount = ((byteCount.intValue + 3) / 4) * 4 - byteCount.intValue; for (int i = 0; i < trailingZeroCount; i++) { data.Add(0); } return(4 + byteCount.intValue + trailingZeroCount); }
public static bool TryReadFrom(byte[] data, ref int index, ref List <int> values) { // Try to get blob byte count. int byteCountPrefixValue; if (!TryReadAndEvaluateByteCountPrefix(data, index, out byteCountPrefixValue)) { return(false); } index += 4; // Adapt list. int valueCount = byteCountPrefixValue / FourByteOscData.byteCount; if (values == null) { values = new List <int>(valueCount); } else { values.Clear(); if (values.Capacity < valueCount) { values.Capacity = valueCount; } } // Fill list. FourByteOscData dataValue; for (int i = 0; i < valueCount; i++) { if (!FourByteOscData.TryReadFrom(data, ref index, out dataValue)) { return(false); } values.Add(dataValue.intValue); } return(true); }
/// <summary> /// Tries to evaluate byte count prefix of a blob and evaluate it. /// </summary> public static bool TryEvaluateByteCount(byte[] data, int index, out int byteCountPrefixValue) { FourByteOscData byteCountPrefix; if (!FourByteOscData.TryReadFrom(data, ref index, out byteCountPrefix)) { // Not enough space for byte count prefix in data array. byteCountPrefixValue = 0; return(false); } int multipleOfFourByteCount = ((byteCountPrefix.intValue + 3) / 4) * 4; // Multiple of four bytes. if (index + multipleOfFourByteCount > data.Length) { // Not enough space for blob in data array. byteCountPrefixValue = 0; return(false); } byteCountPrefixValue = 4 + multipleOfFourByteCount; return(true); }