/* * 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. */ using System; using Token = Lucene.Net.Analysis.Token; using TokenStream = Lucene.Net.Analysis.TokenStream; namespace Lucene.Net.Index { /// A Payload is metadata that can be stored together with each occurrence /// of a term. This metadata is stored inline in the posting list of the /// specific term. ///

/// To store payloads in the index a {@link TokenStream} has to be used that /// produces {@link Token}s containing payload data. ///

/// Use {@link TermPositions#GetPayloadLength()} and {@link TermPositions#GetPayload(byte[], int)} /// to retrieve the payloads from the index.
/// ///

[Serializable] public class Payload : System.ICloneable { /// the byte array containing the payload data protected internal byte[] data; /// the offset within the byte array protected internal int offset; /// the length of the payload data protected internal int length; /// Creates an empty payload and does not allocate a byte array. public Payload() { // nothing to do } /// Creates a new payload with the the given array as data. /// A reference to the passed-in array is held, i. e. no /// copy is made. /// /// /// the data of this payload /// public Payload(byte[] data):this(data, 0, data.Length) { } /// Creates a new payload with the the given array as data. /// A reference to the passed-in array is held, i. e. no /// copy is made. /// /// /// the data of this payload /// /// the offset in the data byte array /// /// the length of the data /// public Payload(byte[] data, int offset, int length) { if (offset < 0 || offset + length > data.Length) { throw new System.ArgumentException(); } this.data = data; this.offset = offset; this.length = length; } /// Sets this payloads data. /// A reference to the passed-in array is held, i. e. no /// copy is made. /// public virtual void SetData(byte[] data) { SetData(data, 0, data.Length); } /// Sets this payloads data. /// A reference to the passed-in array is held, i. e. no /// copy is made. /// public virtual void SetData(byte[] data, int offset, int length) { this.data = data; this.offset = offset; this.length = length; } /// Returns a reference to the underlying byte array /// that holds this payloads data. /// public virtual byte[] GetData() { return this.data; } /// Returns the offset in the underlying byte array public virtual int GetOffset() { return this.offset; } /// Returns the length of the payload data. public virtual int Length() { return this.length; } /// Returns the byte at the given index. public virtual byte ByteAt(int index) { if (0 <= index && index < this.length) { return this.data[this.offset + index]; } throw new System. IndexOutOfRangeException("Index of bound " + index); } /// Allocates a new byte array, copies the payload data into it and returns it. public virtual byte[] ToByteArray() { byte[] retArray = new byte[this.length]; Array.Copy(this.data, this.offset, retArray, 0, this.length); return retArray; } /// Copies the payload data to a byte array. /// /// /// the target byte array /// /// the offset in the target byte array /// public virtual void CopyTo(byte[] target, int targetOffset) { if (this.length > target.Length + targetOffset) { throw new System.IndexOutOfRangeException(); } Array.Copy(this.data, this.offset, target, targetOffset, this.length); } /// Clones this payload by creating a copy of the underlying /// byte array. /// public virtual System.Object Clone() { Payload clone = new Payload(this.ToByteArray()); return clone; } } }