public override Payload Encode(char[] buffer, int offset, int length) { Payload result = new Payload(); float payload = Single.Parse(new string(buffer, offset, length)); // TODO: improve this so that we don't have to new Strings byte[] bytes = PayloadHelper.EncodeFloat(payload); result.SetData(bytes); return result; }
public override Payload Encode(char[] buffer, int offset, int length) { Payload result = new Payload(); int payload = ArrayUtil.ParseInt(buffer, offset, length);//TODO: improve this so that we don't have to new Strings byte[] bytes = PayloadHelper.EncodeInt(payload); result.SetData(bytes); return result; }
public SectionTokenStream(TokenStream tokenStream, int sectionId) : base(tokenStream) { // NOTE: Calling the AddAttribute<T> method failed, so // switched to using AddAttributeImpl. _payloadAtt = new PayloadAttribute(); AddAttributeImpl(_payloadAtt); _payload = EncodeIntPayload(sectionId); }
public NumericPayloadTokenFilter(TokenStream input, float payload, String typeMatch) : base(input) { //Need to encode the payload thePayload = new Payload(PayloadHelper.EncodeFloat(payload)); this.typeMatch = typeMatch; payloadAtt = AddAttribute<IPayloadAttribute>(); typeAtt = AddAttribute<ITypeAttribute>(); }
public sealed override bool IncrementToken() { if (input.IncrementToken()) { byte[] data = new byte[8]; PayloadHelper.EncodeInt(offsetAtt.StartOffset, data, 0); PayloadHelper.EncodeInt(offsetAtt.EndOffset, data, 4); Payload payload = new Payload(data); payAtt.Payload = payload; return true; } else { return false; } }
/// <summary> /// sets meta data /// </summary> /// <param name="data">array of integer metadata indexed by section id</param> public virtual void SetMetaData(int[] data) { byte[] buf = new byte[data.Length * 4]; int i = 0; for (int j = 0; j < data.Length; j++) { int datum = data[j]; buf[i++] = (byte)(datum); buf[i++] = (byte)(((uint)datum) >> 8); buf[i++] = (byte)(((uint)datum) >> 16); buf[i++] = (byte)(((uint)datum) >> 24); } _payload = new Payload(buf); _returnToken = true; }
public override Payload Encode(char[] buffer, int offset, int length) { //what's the most efficient way to get a byte [] from a char[] array //Do we have to go through String? String tmp = new String(buffer, offset, length); Payload result = null;//Can we avoid allocating by knowing where using the new API? try { result = new Payload(encoding.GetBytes(tmp)); } catch (EncoderFallbackException) { //should never hit this, since we get the name from the Charset } return result; }
public static int DecodeIntPayload(Payload payload) { return DecodeIntPayload(payload.GetData(), payload.Offset, payload.Length); }
public virtual void TestPayload() { rnd = NewRandom(); byte[] testData = System.Text.UTF8Encoding.UTF8.GetBytes("This is a test!"); Payload payload = new Payload(testData); Assert.AreEqual(testData.Length, payload.Length(), "Wrong payload length."); // test copyTo() byte[] target = new byte[testData.Length - 1]; try { payload.CopyTo(target, 0); Assert.Fail("Expected exception not thrown"); } catch (System.Exception expected) { // expected exception } target = new byte[testData.Length + 3]; payload.CopyTo(target, 3); for (int i = 0; i < testData.Length; i++) { Assert.AreEqual(testData[i], target[i + 3]); } // test toByteArray() target = payload.ToByteArray(); AssertByteArrayEquals(testData, target); // test byteAt() for (int i = 0; i < testData.Length; i++) { Assert.AreEqual(payload.ByteAt(i), testData[i]); } try { payload.ByteAt(testData.Length + 1); Assert.Fail("Expected exception not thrown"); } catch (System.Exception expected) { // expected exception } Payload clone = (Payload) payload.Clone(); Assert.AreEqual(payload.Length(), clone.Length()); for (int i = 0; i < payload.Length(); i++) { Assert.AreEqual(payload.ByteAt(i), clone.ByteAt(i)); } }
public override bool IncrementToken() { bool hasNext = input.IncrementToken(); if (hasNext) { if (offset + length <= data.Length) { Payload p = null; if (p == null) { p = new Payload(); payloadAtt.SetPayload(p); } p.SetData(data, offset, length); offset += length; } else { payloadAtt.SetPayload(null); } } return hasNext; }
/// <summary> Clones this payload by creating a copy of the underlying /// byte array. /// </summary> public virtual System.Object Clone() { Payload clone = new Payload(this.ToByteArray()); return clone; }
public override Token Next(Token token) { token = input.Next(token); if (token != null) { if (offset + length <= data.Length) { Payload p = null; if (p == null) { p = new Payload(); token.SetPayload(p); } p.SetData(data, offset, length); offset += length; } else { token.SetPayload(null); } } return token; }