http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/ReadOnlyHeapByteBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/ReadOnlyHeapByteBuffer.cs b/src/Lucene.Net/Support/IO/ReadOnlyHeapByteBuffer.cs new file mode 100644 index 0000000..d1d0263 --- /dev/null +++ b/src/Lucene.Net/Support/IO/ReadOnlyHeapByteBuffer.cs @@ -0,0 +1,182 @@ +// This class was sourced from the Apache Harmony project + +namespace Lucene.Net.Support.IO +{ + /* + * 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. + */ + + /// <summary> + /// HeapByteBuffer, ReadWriteHeapByteBuffer and ReadOnlyHeapByteBuffer compose + /// the implementation of array based byte buffers. + /// <para/> + /// ReadOnlyHeapByteBuffer extends HeapByteBuffer with all the write methods + /// throwing read only exception. + /// <para/> + /// This class is sealed final for runtime performance. + /// </summary> + internal sealed class ReadOnlyHeapByteBuffer : HeapByteBuffer + { + internal static ReadOnlyHeapByteBuffer Copy(HeapByteBuffer other, int markOfOther) + { + ReadOnlyHeapByteBuffer buf = new ReadOnlyHeapByteBuffer( + other.backingArray, other.Capacity, other.offset); + buf.limit = other.Limit; + buf.position = other.Position; + buf.mark = markOfOther; + buf.SetOrder(other.Order); + return buf; + } + + internal ReadOnlyHeapByteBuffer(byte[] backingArray, int capacity, int arrayOffset) + : base(backingArray, capacity, arrayOffset) + { + } + + + public override ByteBuffer AsReadOnlyBuffer() + { + return Copy(this, mark); + } + + + public override ByteBuffer Compact() + { + throw new ReadOnlyBufferException(); + } + + public override ByteBuffer Duplicate() + { + return Copy(this, mark); + } + + public override bool IsReadOnly + { + get { return true; } + } + + + protected override byte[] ProtectedArray + { + get { throw new ReadOnlyBufferException(); } + } + + + protected override int ProtectedArrayOffset + { + get { throw new ReadOnlyBufferException(); } + } + + + protected override bool ProtectedHasArray + { + get { return false; } + } + + + public override ByteBuffer Put(byte b) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer Put(int index, byte b) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer Put(byte[] src, int off, int len) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutDouble(double value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutDouble(int index, double value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutSingle(float value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutSingle(int index, float value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutInt32(int value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutInt32(int index, int value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutInt64(int index, long value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutInt64(long value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutInt16(int index, short value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer PutInt16(short value) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer Put(ByteBuffer buf) + { + throw new ReadOnlyBufferException(); + } + + + public override ByteBuffer Slice() + { + ReadOnlyHeapByteBuffer slice = new ReadOnlyHeapByteBuffer(backingArray, + Remaining, offset + position); + slice.order = order; + return slice; + } + } +}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/ReadWriteHeapByteBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/ReadWriteHeapByteBuffer.cs b/src/Lucene.Net/Support/IO/ReadWriteHeapByteBuffer.cs new file mode 100644 index 0000000..7e05712 --- /dev/null +++ b/src/Lucene.Net/Support/IO/ReadWriteHeapByteBuffer.cs @@ -0,0 +1,241 @@ +// This class was sourced from the Apache Harmony project + +using System; + +namespace Lucene.Net.Support.IO +{ + /* + * 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. + */ + + /// <summary> + /// HeapByteBuffer, ReadWriteHeapByteBuffer and ReadOnlyHeapByteBuffer compose + /// the implementation of array based byte buffers. + /// <para/> + /// ReadWriteHeapByteBuffer extends HeapByteBuffer with all the write methods. + /// <para/> + /// This class is marked sealed for runtime performance. + /// </summary> + internal sealed class ReadWriteHeapByteBuffer : HeapByteBuffer + { + internal static ReadWriteHeapByteBuffer Copy(HeapByteBuffer other, int markOfOther) + { + ReadWriteHeapByteBuffer buf = new ReadWriteHeapByteBuffer( + other.backingArray, other.Capacity, other.offset); + buf.limit = other.Limit; + buf.position = other.Position; + buf.mark = markOfOther; + buf.SetOrder(other.Order); + return buf; + } + + internal ReadWriteHeapByteBuffer(byte[] backingArray) + : base(backingArray) + { + } + + internal ReadWriteHeapByteBuffer(int capacity) + : base(capacity) + { + } + + internal ReadWriteHeapByteBuffer(byte[] backingArray, int capacity, int arrayOffset) + : base(backingArray, capacity, arrayOffset) + { + } + + public override ByteBuffer AsReadOnlyBuffer() + { + return ReadOnlyHeapByteBuffer.Copy(this, mark); + } + + public override ByteBuffer Compact() + { + System.Array.Copy(backingArray, position + offset, backingArray, offset, + Remaining); + position = limit - position; + limit = capacity; + mark = UNSET_MARK; + return this; + } + + public override ByteBuffer Duplicate() + { + return Copy(this, mark); + } + + public override bool IsReadOnly + { + get { return false; } + } + + protected override byte[] ProtectedArray + { + get { return backingArray; } + } + + protected override int ProtectedArrayOffset + { + get { return offset; } + } + + protected override bool ProtectedHasArray + { + get { return true; } + } + + public override ByteBuffer Put(byte b) + { + if (position == limit) + { + throw new BufferOverflowException(); + } + backingArray[offset + position++] = b; + return this; + } + + public override ByteBuffer Put(int index, byte b) + { + if (index < 0 || index >= limit) + { + throw new IndexOutOfRangeException(); + } + backingArray[offset + index] = b; + return this; + } + + /* + * Override ByteBuffer.put(byte[], int, int) to improve performance. + * + * (non-Javadoc) + * + * @see java.nio.ByteBuffer#put(byte[], int, int) + */ + + public override ByteBuffer Put(byte[] src, int off, int len) + { + if (off < 0 || len < 0 || (long)off + (long)len > src.Length) + { + throw new IndexOutOfRangeException(); + } + if (len > Remaining) + { + throw new BufferOverflowException(); + } + if (IsReadOnly) + { + throw new ReadOnlyBufferException(); + } + System.Array.Copy(src, off, backingArray, offset + position, len); + position += len; + return this; + } + + public override ByteBuffer PutDouble(double value) + { + return PutInt64(Number.DoubleToRawInt64Bits(value)); + } + + public override ByteBuffer PutDouble(int index, double value) + { + return PutInt64(index, Number.DoubleToRawInt64Bits(value)); + } + + public override ByteBuffer PutSingle(float value) + { + return PutInt32(Number.SingleToInt32Bits(value)); + } + + public override ByteBuffer PutSingle(int index, float value) + { + return PutInt32(index, Number.SingleToInt32Bits(value)); + } + + public override ByteBuffer PutInt32(int value) + { + int newPosition = position + 4; + if (newPosition > limit) + { + throw new BufferOverflowException(); + } + Store(position, value); + position = newPosition; + return this; + } + + public override ByteBuffer PutInt32(int index, int value) + { + if (index < 0 || (long)index + 4 > limit) + { + throw new IndexOutOfRangeException(); + } + Store(index, value); + return this; + } + + public override ByteBuffer PutInt64(int index, long value) + { + if (index < 0 || (long)index + 8 > limit) + { + throw new IndexOutOfRangeException(); + } + Store(index, value); + return this; + } + + public override ByteBuffer PutInt64(long value) + { + int newPosition = position + 8; + if (newPosition > limit) + { + throw new BufferOverflowException(); + } + Store(position, value); + position = newPosition; + return this; + } + + public override ByteBuffer PutInt16(int index, short value) + { + if (index < 0 || (long)index + 2 > limit) + { + throw new IndexOutOfRangeException(); + } + Store(index, value); + return this; + } + + public override ByteBuffer PutInt16(short value) + { + int newPosition = position + 2; + if (newPosition > limit) + { + throw new BufferOverflowException(); + } + Store(position, value); + position = newPosition; + return this; + } + + public override ByteBuffer Slice() + { + ReadWriteHeapByteBuffer slice = new ReadWriteHeapByteBuffer( + backingArray, Remaining, offset + position); + slice.order = order; + return slice; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/ReadWriteLongArrayBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/ReadWriteLongArrayBuffer.cs b/src/Lucene.Net/Support/IO/ReadWriteLongArrayBuffer.cs new file mode 100644 index 0000000..407a1d2 --- /dev/null +++ b/src/Lucene.Net/Support/IO/ReadWriteLongArrayBuffer.cs @@ -0,0 +1,143 @@ +// This class was sourced from the Apache Harmony project + +using System; + +namespace Lucene.Net.Support.IO +{ + /* + * 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. + */ + + /// <summary> + /// LongArrayBuffer, ReadWriteLongArrayBuffer and ReadOnlyLongArrayBuffer compose + /// the implementation of array based long buffers. + /// <para/> + /// ReadWriteLongArrayBuffer extends LongArrayBuffer with all the write methods. + /// <para/> + /// This class is marked final for runtime performance. + /// </summary> + internal sealed class ReadWriteInt64ArrayBuffer : Int64ArrayBuffer + { + internal static ReadWriteInt64ArrayBuffer Copy(Int64ArrayBuffer other, int markOfOther) + { + ReadWriteInt64ArrayBuffer buf = new ReadWriteInt64ArrayBuffer(other + .Capacity, other.backingArray, other.offset); + buf.limit = other.Limit; + buf.position = other.Position; + buf.mark = markOfOther; + return buf; + } + + internal ReadWriteInt64ArrayBuffer(long[] array) + : base(array) + { + } + + internal ReadWriteInt64ArrayBuffer(int capacity) + : base(capacity) + { + } + + internal ReadWriteInt64ArrayBuffer(int capacity, long[] backingArray, int arrayOffset) + : base(capacity, backingArray, arrayOffset) + { + } + + + public override Int64Buffer AsReadOnlyBuffer() + { + throw new NotImplementedException(); + //return ReadOnlyLongArrayBuffer.copy(this, mark); + } + + public override Int64Buffer Compact() + { + System.Array.Copy(backingArray, position + offset, backingArray, offset, + Remaining); + position = limit - position; + limit = capacity; + mark = UNSET_MARK; + return this; + } + + public override Int64Buffer Duplicate() + { + return Copy(this, mark); + } + + public override bool IsReadOnly + { + get { return false; } + } + + protected override long[] ProtectedArray + { + get { return backingArray; } + } + + protected override int ProtectedArrayOffset + { + get { return offset; } + } + + protected override bool ProtectedHasArray + { + get { return true; } + } + + public override Int64Buffer Put(long c) + { + if (position == limit) + { + throw new BufferOverflowException(); + } + backingArray[offset + position++] = c; + return this; + } + + public override Int64Buffer Put(int index, long c) + { + if (index < 0 || index >= limit) + { + throw new IndexOutOfRangeException(); + } + backingArray[offset + index] = c; + return this; + } + + public override Int64Buffer Put(long[] src, int off, int len) + { + int length = src.Length; + if (off < 0 || len < 0 || (long)off + (long)len > length) + { + throw new IndexOutOfRangeException(); + } + if (len > Remaining) + { + throw new BufferOverflowException(); + } + System.Array.Copy(src, off, backingArray, offset + position, len); + position += len; + return this; + } + + public override Int64Buffer Slice() + { + return new ReadWriteInt64ArrayBuffer(Remaining, backingArray, offset + + position); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/LongBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/LongBuffer.cs b/src/Lucene.Net/Support/LongBuffer.cs deleted file mode 100644 index e3d76c1..0000000 --- a/src/Lucene.Net/Support/LongBuffer.cs +++ /dev/null @@ -1,458 +0,0 @@ -/* - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -using System; -using System.Text; - -namespace Lucene.Net.Support -{ - /// <summary> - /// Ported from Java's nio.LongBuffer - /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif - public abstract class Int64Buffer : Buffer, IComparable<Int64Buffer> - { - // These fields are declared here rather than in Heap-X-Buffer in order to - // reduce the number of virtual method invocations needed to access these - // values, which is especially costly when coding small buffers. - // - internal readonly long[] hb; // Non-null only for heap buffers - internal readonly int offset; - internal bool isReadOnly; // Valid only for heap buffers - - /// <summary> - /// Creates a new buffer with the given mark, position, limit, capacity, backing array, and array offset - /// </summary> - public Int64Buffer(int mark, int pos, int lim, int cap, - long[] hb, int offset) - : base(mark, pos, lim, cap) - { - this.hb = hb; - this.offset = offset; - } - - /// <summary> - /// Creates a new buffer with the given mark, position, limit, and capacity - /// </summary> - public Int64Buffer(int mark, int pos, int lim, int cap) - : this(mark, pos, lim, cap, null, 0) - { - } - - public static Int64Buffer Allocate(int capacity) - { - if (capacity < 0) - throw new ArgumentException(); - return new HeapInt64Buffer(capacity, capacity); - } - - - public static Int64Buffer Wrap(long[] array, - int offset, int length) - { - try - { - return new HeapInt64Buffer(array, offset, length); - } -#pragma warning disable 168 - catch (ArgumentException x) -#pragma warning restore 168 - { - throw new ArgumentOutOfRangeException(); - } - } - - public static Int64Buffer Wrap(long[] array) - { - return Wrap(array, 0, array.Length); - } - - - public abstract Int64Buffer Slice(); - - public abstract Int64Buffer Duplicate(); - - public abstract Int64Buffer AsReadOnlyBuffer(); - - public abstract long Get(); - - public abstract Int64Buffer Put(long l); - - public abstract long Get(int index); - - public abstract Int64Buffer Put(int index, long l); - - // -- Bulk get operations -- - - public virtual Int64Buffer Get(long[] dst, int offset, int length) - { - CheckBounds(offset, length, dst.Length); - if (length > Remaining) - throw new BufferUnderflowException(); - int end = offset + length; - for (int i = offset; i < end; i++) - dst[i] = Get(); - return this; - } - - public virtual Int64Buffer Get(long[] dst) - { - return Get(dst, 0, dst.Length); - } - - // -- Bulk put operations -- - - public virtual Int64Buffer Put(Int64Buffer src) - { - if (src == this) - throw new ArgumentException(); - if (IsReadOnly) - throw new ReadOnlyBufferException(); - int n = src.Remaining; - if (n > Remaining) - throw new BufferOverflowException(); - for (int i = 0; i < n; i++) - Put(src.Get()); - return this; - } - - public virtual Int64Buffer Put(long[] src, int offset, int length) - { - CheckBounds(offset, length, src.Length); - if (length > Remaining) - throw new BufferOverflowException(); - int end = offset + length; - for (int i = offset; i < end; i++) - this.Put(src[i]); - return this; - } - - public Int64Buffer Put(long[] src) - { - return Put(src, 0, src.Length); - } - - public override bool HasArray - { - get - { - return (hb != null) && !isReadOnly; - } - } - - public override object Array - { - get - { - if (hb == null) - throw new InvalidOperationException(); - if (isReadOnly) - throw new ReadOnlyBufferException(); - return hb; - } - } - - public override int ArrayOffset - { - get - { - if (hb == null) - throw new InvalidOperationException(); - if (isReadOnly) - throw new ReadOnlyBufferException(); - return offset; - } - } - - public abstract Int64Buffer Compact(); - - //public override bool IsDirect { get; } - - public override string ToString() - { - StringBuilder sb = new StringBuilder(); - sb.Append(GetType().Name); - sb.Append("[pos="); - sb.Append(Position); - sb.Append(" lim="); - sb.Append(Limit); - sb.Append(" cap="); - sb.Append(Capacity); - sb.Append("]"); - return sb.ToString(); - } - - public override int GetHashCode() - { - int h = 1; - int p = Position; - for (int i = Limit - 1; i >= p; i--) - { - h = 31 * h + (int)Get(i); - } - return h; - } - - public override bool Equals(object obj) - { - if (this == obj) - return true; - if (!(obj is Int64Buffer)) - return false; - Int64Buffer that = (Int64Buffer)obj; - if (this.Remaining != that.Remaining) - return false; - int p = this.Position; - for (int i = this.Limit - 1, j = that.Limit - 1; i >= p; i--, j--) - if (!Equals(this.Get(i), that.Get(j))) - return false; - return true; - } - - private static bool Equals(long x, long y) - { - return x == y; - } - - public int CompareTo(Int64Buffer other) - { - int n = this.Position + Math.Min(this.Remaining, other.Remaining); - for (int i = this.Position, j = other.Position; i < n; i++, j++) - { - int cmp = Compare(this.Get(i), other.Get(j)); - if (cmp != 0) - return cmp; - } - return this.Remaining - other.Remaining; - } - - private static int Compare(long x, long y) - { - // from Long.compare(x, y) - return (x < y) ? -1 : ((x == y) ? 0 : 1); - } - - // -- Other char stuff -- - - // (empty) - - // -- Other byte stuff: Access to binary data -- - - public abstract ByteOrder Order { get; } - - - /// <summary> - /// NOTE: This was HeapLongBuffer in the JDK - /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif - public class HeapInt64Buffer : Int64Buffer - { - // For speed these fields are actually declared in X-Buffer; - // these declarations are here as documentation - /* - - protected final long[] hb; - protected final int offset; - - */ - - internal HeapInt64Buffer(int cap, int lim) - : base(-1, 0, lim, cap, new long[cap], 0) - { - /* - hb = new long[cap]; - offset = 0; - */ - } - - internal HeapInt64Buffer(long[] buf, int off, int len) - : base(-1, off, off + len, buf.Length, buf, 0) - { - /* - hb = buf; - offset = 0; - */ - } - - protected HeapInt64Buffer(long[] buf, - int mark, int pos, int lim, int cap, - int off) - : base(mark, pos, lim, cap, buf, off) - { - /* - hb = buf; - offset = off; - */ - } - - public override Int64Buffer Slice() - { - return new HeapInt64Buffer(hb, - -1, - 0, - this.Remaining, - this.Remaining, - this.Position + offset); - } - - public override Int64Buffer Duplicate() - { - return new HeapInt64Buffer(hb, - this.MarkValue, - this.Position, - this.Limit, - this.Capacity, - offset); - } - - public override Int64Buffer AsReadOnlyBuffer() - { - throw new NotImplementedException(); - //return new HeapLongBufferR(hb, - // this.MarkValue(), - // this.Position, - // this.Limit, - // this.Capacity, - // offset); - } - - protected virtual int Ix(int i) - { - return i + offset; - } - - public override long Get() - { - return hb[Ix(NextGetIndex())]; - } - - public override long Get(int index) - { - return hb[Ix(CheckIndex(index))]; - } - - public override Int64Buffer Get(long[] dst, int offset, int length) - { - CheckBounds(offset, length, dst.Length); - if (length > Remaining) - throw new BufferUnderflowException(); - System.Array.Copy(hb, Ix(Position), dst, offset, length); - SetPosition(Position + length); - return this; - } - - - public override bool IsDirect - { - get - { - return false; - } - } - - public override bool IsReadOnly - { - get - { - return false; - } - } - - public override Int64Buffer Put(long l) - { - hb[Ix(NextPutIndex())] = l; - return this; - } - - public override Int64Buffer Put(int index, long l) - { - hb[Ix(CheckIndex(index))] = l; - return this; - } - - public override Int64Buffer Put(long[] src, int offset, int length) - { - - CheckBounds(offset, length, src.Length); - if (length > Remaining) - throw new BufferOverflowException(); - System.Array.Copy(src, offset, hb, Ix(Position), length); - SetPosition(Position + length); - return this; - } - - public override Int64Buffer Put(Int64Buffer src) - { - - if (src is HeapInt64Buffer) { - if (src == this) - throw new ArgumentException(); - HeapInt64Buffer sb = (HeapInt64Buffer)src; - int n = sb.Remaining; - if (n > Remaining) - throw new BufferOverflowException(); - System.Array.Copy(sb.hb, sb.Ix(sb.Position), - hb, Ix(Position), n); - sb.SetPosition(sb.Position + n); - SetPosition(Position + n); - } else if (src.IsDirect) - { - int n = src.Remaining; - if (n > Remaining) - throw new BufferOverflowException(); - src.Get(hb, Ix(Position), n); - SetPosition(Position + n); - } - else - { - base.Put(src); - } - return this; - } - - public override Int64Buffer Compact() - { - System.Array.Copy(hb, Ix(Position), hb, Ix(0), Remaining); - SetPosition(Remaining); - SetLimit(Capacity); - DiscardMark(); - return this; - } - - public override ByteOrder Order - { - get - { - throw new NotImplementedException(); - //return ByteOrder.nativeOrder(); - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/MemoryMappedFileByteBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/MemoryMappedFileByteBuffer.cs b/src/Lucene.Net/Support/MemoryMappedFileByteBuffer.cs deleted file mode 100644 index a48b8b8..0000000 --- a/src/Lucene.Net/Support/MemoryMappedFileByteBuffer.cs +++ /dev/null @@ -1,438 +0,0 @@ -using System; -using System.IO.MemoryMappedFiles; - -namespace Lucene.Net.Support -{ - /* - * 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. - */ - - internal sealed class MemoryMappedFileByteBuffer : ByteBuffer, IDisposable - { - private MemoryMappedViewAccessor _accessor; - private readonly int offset; - new private bool bigEndian = true; - - public MemoryMappedFileByteBuffer(MemoryMappedViewAccessor accessor, int mark, int pos, int lim, int cap) - : base(mark, pos, lim, cap) - { - _accessor = accessor; - } - - public MemoryMappedFileByteBuffer(MemoryMappedViewAccessor accessor, int mark, int pos, int lim, int cap, int offset) - : this(accessor, mark, pos, lim, cap) - { - this.offset = offset; - } - - public override ByteBuffer Slice() - { - return new MemoryMappedFileByteBuffer(_accessor, -1, 0, Remaining, Remaining); - } - - public override ByteBuffer Duplicate() - { - return new MemoryMappedFileByteBuffer(_accessor, MarkValue, Position, Limit, Capacity); - } - - public override ByteBuffer AsReadOnlyBuffer() - { - throw new NotImplementedException(); - } - - - private int Ix(int i) - { - return i + offset; - } - - public override byte Get() - { - return _accessor.ReadByte(Ix(NextGetIndex())); - } - - public override byte Get(int index) - { - return _accessor.ReadByte(Ix(CheckIndex(index))); - } - -#if !NETSTANDARD - // Implementation provided by Vincent Van Den Berghe: http://git.net/ml/general/2017-02/msg31639.html - public override ByteBuffer Get(byte[] dst, int offset, int length) - { - CheckBounds(offset, length, dst.Length); - if (length > Remaining) - { - throw new BufferUnderflowException(); - } - // we need to check for 0-length reads, since - // ReadArray will throw an ArgumentOutOfRange exception if position is at - // the end even when nothing is read - if (length > 0) - { - _accessor.ReadArray(Ix(NextGetIndex(length)), dst, offset, length); - } - - return this; - } -#endif - - public override bool IsDirect - { - get { return false; } - } - - public override bool IsReadOnly - { - get { return false; } - } - - public override ByteBuffer Put(byte b) - { - _accessor.Write(Ix(NextPutIndex()), b); - return this; - } - - public override ByteBuffer Put(int index, byte b) - { - _accessor.Write(Ix(CheckIndex(index)), b); - return this; - } - -#if !NETSTANDARD - // Implementation provided by Vincent Van Den Berghe: http://git.net/ml/general/2017-02/msg31639.html - public override ByteBuffer Put(byte[] src, int offset, int length) - { - CheckBounds(offset, length, src.Length); - if (length > Remaining) - { - throw new BufferOverflowException(); - } - // we need to check for 0-length writes, since - // ReadArray will throw an ArgumentOutOfRange exception if position is at - // the end even when nothing is read - if (length > 0) - { - _accessor.WriteArray(Ix(NextPutIndex(length)), src, offset, length); - } - return this; - } -#endif - - public override ByteBuffer Compact() - { - throw new NotSupportedException(); - } - - internal override byte _get(int i) - { - throw new NotSupportedException(); - } - - internal override void _put(int i, byte b) - { - throw new NotSupportedException(); - } - - - public override char GetChar() - { - var littleEndian = _accessor.ReadChar(Ix(NextGetIndex(2))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - public override char GetChar(int index) - { - var littleEndian = _accessor.ReadChar(Ix(CheckIndex(index, 2))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - public override ByteBuffer PutChar(char value) - { - _accessor.Write(Ix(NextPutIndex(2)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - - - public override ByteBuffer PutChar(int index, char value) - { - _accessor.Write(Ix(CheckIndex(index, 2)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - /// <summary> - /// NOTE: This was getShort() in the JDK - /// </summary> - public override short GetInt16() - { - var littleEndian = _accessor.ReadInt16(Ix(NextGetIndex(2))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - /// <summary> - /// NOTE: This was getShort() in the JDK - /// </summary> - public override short GetInt16(int index) - { - var littleEndian = _accessor.ReadInt16(Ix(CheckIndex(index, 2))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - /// <summary> - /// NOTE: This was putShort() in the JDK - /// </summary> - public override ByteBuffer PutInt16(short value) - { - _accessor.Write(Ix(NextPutIndex(2)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - /// <summary> - /// NOTE: This was putShort() in the JDK - /// </summary> - public override ByteBuffer PutInt16(int index, short value) - { - _accessor.Write(Ix(CheckIndex(index, 2)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - /// <summary> - /// NOTE: This was getInt() in the JDK - /// </summary> - public override int GetInt32() - { - var littleEndian = _accessor.ReadInt32(Ix(NextGetIndex(4))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - /// <summary> - /// NOTE: This was getInt() in the JDK - /// </summary> - public override int GetInt32(int index) - { - var littleEndian = _accessor.ReadInt32(Ix(CheckIndex(index, 4))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - /// <summary> - /// NOTE: This was putInt() in the JDK - /// </summary> - public override ByteBuffer PutInt32(int value) - { - _accessor.Write(Ix(NextPutIndex(4)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - - /// <summary> - /// NOTE: This was putInt() in the JDK - /// </summary> - public override ByteBuffer PutInt32(int index, int value) - { - _accessor.Write(Ix(CheckIndex(index, 4)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - /// <summary> - /// NOTE: This was getLong() in the JDK - /// </summary> - public override long GetInt64() - { - var littleEndian = _accessor.ReadInt64(Ix(NextGetIndex(8))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - /// <summary> - /// NOTE: This was getLong() in the JDK - /// </summary> - public override long GetInt64(int index) - { - var littleEndian = _accessor.ReadInt64(Ix(CheckIndex(index, 8))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - /// <summary> - /// NOTE: This was putLong() in the JDK - /// </summary> - public override ByteBuffer PutInt64(long value) - { - _accessor.Write(Ix(NextPutIndex(8)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - /// <summary> - /// NOTE: This was putLong() in the JDK - /// </summary> - public override ByteBuffer PutInt64(int index, long value) - { - _accessor.Write(Ix(CheckIndex(index, 8)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - /// <summary> - /// NOTE: This was getFloat() in the JDK - /// </summary> - public override float GetSingle() - { - byte[] temp = new byte[4]; - temp[0] = _accessor.ReadByte(Ix(NextGetIndex())); - temp[1] = _accessor.ReadByte(Ix(NextGetIndex())); - temp[2] = _accessor.ReadByte(Ix(NextGetIndex())); - temp[3] = _accessor.ReadByte(Ix(NextGetIndex())); - if (bigEndian) - { - System.Array.Reverse(temp); - } - return BitConverter.ToSingle(temp, 0); - } - - /// <summary> - /// NOTE: This was getFloat() in the JDK - /// </summary> - public override float GetSingle(int index) - { - byte[] temp = new byte[4]; - temp[0] = _accessor.ReadByte(Ix(NextGetIndex(index))); - temp[1] = _accessor.ReadByte(Ix(NextGetIndex())); - temp[2] = _accessor.ReadByte(Ix(NextGetIndex())); - temp[3] = _accessor.ReadByte(Ix(NextGetIndex())); - if (bigEndian) - { - System.Array.Reverse(temp); - } - return BitConverter.ToSingle(temp, 0); - } - - /// <summary> - /// NOTE: This was putFloat() in the JDK - /// </summary> - public override ByteBuffer PutSingle(float value) - { - var bytes = BitConverter.GetBytes(value); - - if (bigEndian) - { - System.Array.Reverse(bytes); - } - - _accessor.Write(Ix(NextPutIndex()), bytes[0]); - _accessor.Write(Ix(NextPutIndex()), bytes[1]); - _accessor.Write(Ix(NextPutIndex()), bytes[2]); - _accessor.Write(Ix(NextPutIndex()), bytes[3]); - return this; - } - - /// <summary> - /// NOTE: This was putFloat() in the JDK - /// </summary> - public override ByteBuffer PutSingle(int index, float value) - { - var bytes = BitConverter.GetBytes(value); - - if (bigEndian) - { - System.Array.Reverse(bytes); - } - - _accessor.Write(Ix(NextPutIndex(index)), bytes[0]); - _accessor.Write(Ix(NextPutIndex()), bytes[1]); - _accessor.Write(Ix(NextPutIndex()), bytes[2]); - _accessor.Write(Ix(NextPutIndex()), bytes[3]); - return this; - } - - public override double GetDouble() - { - var littleEndian = _accessor.ReadDouble(Ix(NextGetIndex(8))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - public override double GetDouble(int index) - { - var littleEndian = _accessor.ReadDouble(Ix(CheckIndex(index, 8))); - if (bigEndian) - { - return Number.FlipEndian(littleEndian); - } - return littleEndian; - } - - public override ByteBuffer PutDouble(double value) - { - _accessor.Write(Ix(NextPutIndex(8)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - public override ByteBuffer PutDouble(int index, double value) - { - _accessor.Write(Ix(CheckIndex(index, 8)), bigEndian ? Number.FlipEndian(value) : value); - return this; - } - - public void Dispose() - { - if (_accessor != null) - _accessor.Dispose(); - - _accessor = null; - } - - /// <summary> - /// NOTE: This was asLongBuffer() in the JDK - /// </summary> - public override Int64Buffer AsInt64Buffer() - { - throw new NotSupportedException(); - } - } -} \ No newline at end of file
