// ----------------------------------------------------------------------- // // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to You 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. // // // ----------------------------------------------------------------------- namespace Lucene.Net.Index { using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using Lucene.Net.Support; using Util; /// /// TODO: port /// public class Payload : ICloneable { private byte[] data; /// /// Initializes a new instance of the class. This /// constructor will create an empty payload with a null for the . /// public Payload() { this.Data = new byte[] { }; } /// /// Initializes a new instance of the class. /// /// The data. /// The offset. /// The limit. public Payload(byte[] data, int offset = 0, int limit = 0) { this.SetData(data, offset, limit); } /// /// Gets or sets the offset. /// /// The offset. public int Offset { get; set; } /// /// Gets or sets the length. /// /// The length. public int Length { get; protected set; } /// /// Gets or sets the data. /// /// The data. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Lucene code base uses byte[] through out.")] public byte[] Data { get { return this.data; } set { this.SetData(value); } } /// /// Retrieves the byte at the given index. Similar to CharAt /// /// The index. /// An instance of . /// /// Thrown when the specified index is less than 0 or greater than /// or equal to the /// public virtual byte ByteAt(int index) { if (0 <= index && index < this.Length) return this.Data[this.Offset + index]; throw new ArgumentOutOfRangeException( "index", "The index must be greater than 0 and less than the length '{0}'. " + "The index was '{1}'. ".Inject(this.Length, index)); } /// /// Creates a clone of the object, generally shallow. /// /// an the clone of the current instance. public object Clone() { Payload clone = (Payload)this.MemberwiseClone(); if (this.Offset == 0 && this.Length == this.Data.Length) { clone.data = new byte[this.Data.Length]; this.CopyTo(clone.data); } else { clone.data = this.ToByteArray(); clone.Offset = 0; } return clone; } /// /// Copies the payload data to a byte array. /// /// The target. /// The offset of the target. The default is 0. /// /// Thrown when is null. /// /// /// Thrown when this instances is greater than /// the combined target length and offset. /// public virtual void CopyTo(byte[] target, int offset = 0) { if (target == null) throw new ArgumentNullException("target"); if (this.Length > target.Length + offset) { var message = "The combined target length and offset '{0}' must be smaller the payload length '{1}' " .Inject((target.Length + offset), this.Length); throw new ArgumentOutOfRangeException("target", message); } Array.Copy(this.Data, this.Offset, target, offset, this.Length); } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) { if (obj == this) return true; Payload payload = obj as Payload; if (payload == null) return false; if (this.Length == payload.Length) { for (int i = 0; i < this.Length; i++) { if (this.Data[this.Offset + i] != payload.Data[payload.Offset + i]) return false; } return true; } return false; } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { return this.Data.CreateHashCode(this.Offset, this.Offset + this.Length); } /// /// Sets the payload data. A reference to the passed-in aray is held but /// not copied /// /// The data. /// The offset of the data. /// The length of the data. /// /// Thrown when is less than 0 /// or when the and /// is greater than 's length. /// public void SetData(byte[] data, int offset = 0, int length = 0) { if (offset < 0 || (offset + length) > data.Length) throw new ArgumentException( "The offset must be 0 or greater and the offset and length " + "combined must be less that length of byte[]." + " The offset was '{0}'. ".Inject(offset), "offset"); this.data = data; this.Offset = offset; this.Length = length == 0 ? data.Length : length; } /// /// Allocates a new byte array. Then copies the payload data into the new array /// and returns it. /// /// An instance of . public virtual byte[] ToByteArray() { byte[] copy = new byte[this.Length]; Array.Copy(this.Data, this.Offset, copy, 0, this.Length); return copy; } } }