internal void SetAttribute <T>(RadiusAttributeParser <T> parser, int index, T value) { // check for not read-only EnsureWritable(); // determine offset and old length var offset = OffsetsFromParser(parser)[index]; var oldLength = buffer[offset + 1]; // parse value into byte array var attribute = parser.Write(value); var newLength = attribute.Length + 2; if (newLength > byte.MaxValue) { throw new ArgumentOutOfRangeException("value"); } // prepare buffer if necessary if (oldLength != newLength) { CorrectOffsets(offset + oldLength, newLength - oldLength); } // write attribute buffer[offset] = parser.Type; buffer[offset + 1] = (byte)newLength; Array.Copy(attribute, 0, buffer, offset + 2, attribute.Length); }
internal void InsertAttribute <T>(RadiusAttributeParser <T> parser, int index, T item) { // check for not read-only EnsureWritable(); // determine offset var offsets = OffsetsFromParser(parser); var offset = index == offsets.Count ? Length : offsets[index]; // parse value into a byte array var attribute = parser.Write(item); var length = attribute.Length + 2; if (length > byte.MaxValue) { throw new ArgumentOutOfRangeException("item"); } // prepare buffer CorrectOffsets(offset, length); // write attribute buffer[offset] = parser.Type; buffer[offset + 1] = (byte)length; Array.Copy(attribute, 0, buffer, offset + 2, length - 2); // store offset offsets.Insert(index, offset); }