http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/ByteOrder.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/ByteOrder.cs b/src/Lucene.Net/Support/IO/ByteOrder.cs new file mode 100644 index 0000000..a8453f3 --- /dev/null +++ b/src/Lucene.Net/Support/IO/ByteOrder.cs @@ -0,0 +1,81 @@ +// 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> + /// Defines byte order constants. + /// </summary> + public sealed class ByteOrder + { + /// <summary> + /// This constant represents big endian. + /// </summary> + public static readonly ByteOrder BIG_ENDIAN = new ByteOrder("BIG_ENDIAN"); //$NON-NLS-1$ + + /// <summary> + /// This constant represents little endian. + /// </summary> + public static readonly ByteOrder LITTLE_ENDIAN = new ByteOrder("LITTLE_ENDIAN"); //$NON-NLS-1$ + + private static readonly ByteOrder NATIVE_ORDER; + + static ByteOrder() + { + // Read endianness from the current system. + if (BitConverter.IsLittleEndian) + { + NATIVE_ORDER = LITTLE_ENDIAN; + } + else + { + NATIVE_ORDER = BIG_ENDIAN; + } + } + + /// <summary> + /// Returns the current platform byte order. + /// </summary> + public static ByteOrder NativeOrder + { + get { return NATIVE_ORDER; } + } + + private readonly string name; + + private ByteOrder(string name) + { + this.name = name; + } + + /// <summary> + /// Returns a string that describes this object. + /// </summary> + /// <returns> + /// "BIG_ENDIAN" for <see cref="ByteOrder.BIG_ENDIAN"/> objects, + /// "LITTLE_ENDIAN" for <see cref="ByteOrder.LITTLE_ENDIAN"/> objects. + /// </returns> + public override string ToString() + { + return name; + } + } +}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/Endianness.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/Endianness.cs b/src/Lucene.Net/Support/IO/Endianness.cs new file mode 100644 index 0000000..39499a0 --- /dev/null +++ b/src/Lucene.Net/Support/IO/Endianness.cs @@ -0,0 +1,49 @@ +// 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. + */ + + public sealed class Endianness + { + public static readonly Endianness BIG_ENDIAN = new Endianness("BIG_ENDIAN"); + + public static readonly Endianness LITTLE_ENDIAN = new Endianness("LITTLE_ENDIAN"); + + // The string used to display the mapping mode. + private readonly string displayName; + + /// <summary> + /// Private constructor prevents others creating new Endians. + /// </summary> + /// <param name="displayName"></param> + private Endianness(string displayName) + { + this.displayName = displayName; + } + + /// <summary> + /// Answers a string version of the endianness + /// </summary> + /// <returns>the mode string.</returns> + public override string ToString() + { + return displayName; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/FileStreamExtensions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/FileStreamExtensions.cs b/src/Lucene.Net/Support/IO/FileStreamExtensions.cs new file mode 100644 index 0000000..4d686b9 --- /dev/null +++ b/src/Lucene.Net/Support/IO/FileStreamExtensions.cs @@ -0,0 +1,56 @@ +using System.IO; + +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. + */ + + public static class FileStreamExtensions + { + private static object _fsReadLock = new object(); + + //Reads bytes from the Filestream into the bytebuffer + public static int Read(this FileStream file, ByteBuffer dst, long position) + { + lock (_fsReadLock) + { + // TODO: check this logic, could probably optimize + if (position >= file.Length) + return 0; + + var original = file.Position; + + file.Seek(position, SeekOrigin.Begin); + + int count = 0; + + for (int i = dst.Position; i < dst.Limit; i++) + { + int v = file.ReadByte(); + if (v == -1) + break; + dst.Put((byte) v); + count++; + } + + file.Seek(original, SeekOrigin.Begin); + + return count; + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/HeapByteBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/HeapByteBuffer.cs b/src/Lucene.Net/Support/IO/HeapByteBuffer.cs new file mode 100644 index 0000000..0c9a5fc --- /dev/null +++ b/src/Lucene.Net/Support/IO/HeapByteBuffer.cs @@ -0,0 +1,383 @@ +// 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/> + /// HeapByteBuffer implements all the shared readonly methods and is extended by + /// the other two classes. + /// <para/> + /// All methods are sealed for runtime performance. + /// </summary> + internal abstract class HeapByteBuffer : ByteBuffer + { + protected internal readonly byte[] backingArray; + + protected internal readonly int offset; + + internal HeapByteBuffer(byte[] backingArray) + : this(backingArray, backingArray.Length, 0) + { + } + + internal HeapByteBuffer(int capacity) + : this(new byte[capacity], capacity, 0) + { + } + + internal HeapByteBuffer(byte[] backingArray, int capacity, int offset) + : base(capacity) + { + this.backingArray = backingArray; + this.offset = offset; + + if (offset + capacity > backingArray.Length) + { + throw new IndexOutOfRangeException(); + } + } + + /* + * Override ByteBuffer.get(byte[], int, int) to improve performance. + * + * (non-Javadoc) + * + * @see java.nio.ByteBuffer#get(byte[], int, int) + */ + + public override sealed ByteBuffer Get(byte[] dest, int off, int len) + { + int length = dest.Length; + if (off < 0 || len < 0 || (long)off + (long)len > length) + { + throw new IndexOutOfRangeException(); + } + if (len > Remaining) + { + throw new BufferUnderflowException(); + } + System.Array.Copy(backingArray, offset + position, dest, off, len); + position += len; + return this; + } + + public override sealed byte Get() + { + if (position == limit) + { + throw new BufferUnderflowException(); + } + return backingArray[offset + position++]; + } + + + public override sealed byte Get(int index) + { + if (index < 0 || index >= limit) + { + throw new IndexOutOfRangeException(); + } + return backingArray[offset + index]; + } + + + public override sealed double GetDouble() + { + return BitConverter.Int64BitsToDouble(GetInt64()); + } + + + public override sealed double GetDouble(int index) + { + return BitConverter.Int64BitsToDouble(GetInt64(index)); + } + + + public override sealed float GetSingle() + { + return Number.Int32BitsToSingle(GetInt32()); + } + + + public override sealed float GetSingle(int index) + { + return Number.Int32BitsToSingle(GetInt32(index)); + } + + + public override sealed int GetInt32() + { + int newPosition = position + 4; + if (newPosition > limit) + { + throw new BufferUnderflowException(); + } + int result = LoadInt32(position); + position = newPosition; + return result; + } + + + public override sealed int GetInt32(int index) + { + if (index < 0 || index + 4 > limit) + { + throw new IndexOutOfRangeException(); + } + return LoadInt32(index); + } + + + public override sealed long GetInt64() + { + int newPosition = position + 8; + if (newPosition > limit) + { + throw new BufferUnderflowException(); + } + long result = LoadInt64(position); + position = newPosition; + return result; + } + + + public override sealed long GetInt64(int index) + { + if (index < 0 || index + 8 > limit) + { + throw new IndexOutOfRangeException(); + } + return LoadInt64(index); + } + + + public override sealed short GetInt16() + { + int newPosition = position + 2; + if (newPosition > limit) + { + throw new BufferUnderflowException(); + } + short result = LoadInt16(position); + position = newPosition; + return result; + } + + + public override sealed short GetInt16(int index) + { + if (index < 0 || index + 2 > limit) + { + throw new IndexOutOfRangeException(); + } + return LoadInt16(index); + } + + + public override sealed bool IsDirect + { + get { return false; } + } + + protected int LoadInt32(int index) + { + int baseOffset = offset + index; + int bytes = 0; + if (order == Endianness.BIG_ENDIAN) + { + for (int i = 0; i < 4; i++) + { + bytes = bytes << 8; + bytes = bytes | (backingArray[baseOffset + i] & 0xFF); + } + } + else + { + for (int i = 3; i >= 0; i--) + { + bytes = bytes << 8; + bytes = bytes | (backingArray[baseOffset + i] & 0xFF); + } + } + return bytes; + } + + protected long LoadInt64(int index) + { + int baseOffset = offset + index; + long bytes = 0; + if (order == Endianness.BIG_ENDIAN) + { + for (int i = 0; i < 8; i++) + { + bytes = bytes << 8; + bytes = bytes | (uint)(backingArray[baseOffset + i] & 0xFF); + } + } + else + { + for (int i = 7; i >= 0; i--) + { + bytes = bytes << 8; + bytes = bytes | (uint)(backingArray[baseOffset + i] & 0xFF); + } + } + return bytes; + } + + protected short LoadInt16(int index) + { + int baseOffset = offset + index; + short bytes = 0; + if (order == Endianness.BIG_ENDIAN) + { + bytes = (short)(backingArray[baseOffset] << 8); + bytes |= (short)(backingArray[baseOffset + 1] & 0xFF); + } + else + { + bytes = (short)(backingArray[baseOffset + 1] << 8); + bytes |= (short)(backingArray[baseOffset] & 0xFF); + } + return bytes; + } + + protected void Store(int index, int value) + { + int baseOffset = offset + index; + if (order == Endianness.BIG_ENDIAN) + { + for (int i = 3; i >= 0; i--) + { + backingArray[baseOffset + i] = (byte)(value & 0xFF); + value = value >> 8; + } + } + else + { + for (int i = 0; i <= 3; i++) + { + backingArray[baseOffset + i] = (byte)(value & 0xFF); + value = value >> 8; + } + } + } + + protected void Store(int index, long value) + { + int baseOffset = offset + index; + if (order == Endianness.BIG_ENDIAN) + { + for (int i = 7; i >= 0; i--) + { + backingArray[baseOffset + i] = (byte)(value & 0xFF); + value = value >> 8; + } + } + else + { + for (int i = 0; i <= 7; i++) + { + backingArray[baseOffset + i] = (byte)(value & 0xFF); + value = value >> 8; + } + } + } + + protected void Store(int index, short value) + { + int baseOffset = offset + index; + if (order == Endianness.BIG_ENDIAN) + { + backingArray[baseOffset] = (byte)((value >> 8) & 0xFF); + backingArray[baseOffset + 1] = (byte)(value & 0xFF); + } + else + { + backingArray[baseOffset + 1] = (byte)((value >> 8) & 0xFF); + backingArray[baseOffset] = (byte)(value & 0xFF); + } + } + + + //public override sealed CharBuffer AsCharBuffer() + // { + // return CharToByteBufferAdapter.Wrap(this); + // } + + + //public override sealed DoubleBuffer AsDoubleBuffer() + // { + // return DoubleToByteBufferAdapter.Wrap(this); + // } + + + //public override sealed SingleBuffer AsSingleBuffer() + // { + // return SingleToByteBufferAdapter.Wrap(this); + // } + + + //public override sealed Int32Buffer AsInt32Buffer() + // { + // return Int32ToByteBufferAdapter.Wrap(this); + // } + + + public override sealed Int64Buffer AsInt64Buffer() + { + return Int64ToByteBufferAdapter.Wrap(this); + } + + + //public override sealed Int16Buffer AsInt16Buffer() + // { + // return Int16ToByteBufferAdapter.Wrap(this); + // } + + + public override sealed char GetChar() + { + return (char)GetInt16(); + } + + + public override sealed char GetChar(int index) + { + return (char)GetInt16(index); + } + + + public override sealed ByteBuffer PutChar(char value) + { + return PutInt16((short)value); + } + + + public override sealed ByteBuffer PutChar(int index, char value) + { + return PutInt16(index, (short)value); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/LongArrayBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/LongArrayBuffer.cs b/src/Lucene.Net/Support/IO/LongArrayBuffer.cs new file mode 100644 index 0000000..5998c07 --- /dev/null +++ b/src/Lucene.Net/Support/IO/LongArrayBuffer.cs @@ -0,0 +1,105 @@ +// 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/> + /// LongArrayBuffer implements all the shared readonly methods and is extended by + /// the other two classes. + /// <para/> + /// All methods are marked final for runtime performance. + /// </summary> + internal abstract class Int64ArrayBuffer : Int64Buffer + { + protected internal readonly long[] backingArray; + + protected internal readonly int offset; + + internal Int64ArrayBuffer(long[] array) + : this(array.Length, array, 0) + { + } + + internal Int64ArrayBuffer(int capacity) + : this(capacity, new long[capacity], 0) + { + } + + internal Int64ArrayBuffer(int capacity, long[] backingArray, int offset) + : base(capacity) + { + this.backingArray = backingArray; + this.offset = offset; + } + + + public override sealed long Get() + { + if (position == limit) + { + throw new BufferUnderflowException(); + } + return backingArray[offset + position++]; + } + + + public override sealed long Get(int index) + { + if (index < 0 || index >= limit) + { + throw new IndexOutOfRangeException(); + } + return backingArray[offset + index]; + } + + + public override sealed Int64Buffer Get(long[] dest, int off, int len) + { + int length = dest.Length; + if (off < 0 || len < 0 || (long)len + (long)off > length) + { + throw new IndexOutOfRangeException(); + } + if (len > Remaining) + { + throw new BufferUnderflowException(); + } + System.Array.Copy(backingArray, offset + position, dest, off, len); + position += len; + return this; + } + + + public override sealed bool IsDirect + { + get { return false; } + } + + + public override sealed ByteOrder Order + { + get { return ByteOrder.NativeOrder; } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/LongBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/LongBuffer.cs b/src/Lucene.Net/Support/IO/LongBuffer.cs new file mode 100644 index 0000000..6285743 --- /dev/null +++ b/src/Lucene.Net/Support/IO/LongBuffer.cs @@ -0,0 +1,739 @@ +// This class was sourced from the Apache Harmony project + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using System.Text; + +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> + /// A buffer of longs. + /// <para/> + /// A long buffer can be created in either of the following ways: + /// <list type="bullet"> + /// <item><see cref="Allocate(int)"/> a new long array and create a buffer + /// based on it</item> + /// <item><see cref="Wrap(long[])"/> an existing long array to create a new + /// buffer</item> + /// </list> + /// </summary> +#if FEATURE_SERIALIZABLE + [Serializable] +#endif + public abstract class Int64Buffer : Buffer, IComparable<Int64Buffer> + { + /// <summary> + /// Creates a long buffer based on a newly allocated long array. + /// </summary> + /// <param name="capacity">the capacity of the new buffer.</param> + /// <returns>the created long buffer.</returns> + /// <exception cref="ArgumentException">if <paramref name="capacity"/> is less than zero.</exception> + public static Int64Buffer Allocate(int capacity) + { + if (capacity < 0) + { + throw new ArgumentException(); + } + return new ReadWriteInt64ArrayBuffer(capacity); + } + + /// <summary> + /// Creates a new long buffer by wrapping the given long array. + /// <para/> + /// Calling this method has the same effect as + /// <c>Wrap(array, 0, array.Length)</c>. + /// </summary> + /// <param name="array">the long array which the new buffer will be based on.</param> + /// <returns>the created long buffer.</returns> + public static Int64Buffer Wrap(long[] array) + { + return Wrap(array, 0, array != null ? array.Length : 0); + } + + public static Int64Buffer Wrap(long[] array, int start, int len) + { + if (array == null) + { + throw new ArgumentNullException(); + } + if (start < 0 || len < 0 || (long)len + (long)start > array.Length) + { + throw new IndexOutOfRangeException(); + } + + Int64Buffer buf = new ReadWriteInt64ArrayBuffer(array); + buf.position = start; + buf.limit = start + len; + + return buf; + } + + + /// <summary> + /// Constructs a <see cref="Int64Buffer"/> with given capacity. + /// </summary> + internal Int64Buffer(int capacity) + : base(capacity) + { + } + + [WritableArray] + [SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")] + public long[] Array + { + get { return ProtectedArray; } + } + + public int ArrayOffset + { + get { return ProtectedArrayOffset; } + } + + public abstract Int64Buffer AsReadOnlyBuffer(); + + public abstract Int64Buffer Compact(); + + public int CompareTo(Int64Buffer otherBuffer) + { + int compareRemaining = (Remaining < otherBuffer.Remaining) ? Remaining + : otherBuffer.Remaining; + int thisPos = position; + int otherPos = otherBuffer.position; + long thisByte, otherByte; + while (compareRemaining > 0) + { + thisByte = Get(thisPos); + otherByte = otherBuffer.Get(otherPos); + if (thisByte != otherByte) + { + return thisByte < otherByte ? -1 : 1; + } + thisPos++; + otherPos++; + compareRemaining--; + } + return Remaining - otherBuffer.Remaining; + } + + public abstract Int64Buffer Duplicate(); + + public override bool Equals(object other) + { + if (!(other is Int64Buffer)) { + return false; + } + Int64Buffer otherBuffer = (Int64Buffer)other; + + if (Remaining != otherBuffer.Remaining) + { + return false; + } + + int myPosition = position; + int otherPosition = otherBuffer.position; + bool equalSoFar = true; + while (equalSoFar && (myPosition < limit)) + { + equalSoFar = Get(myPosition++) == otherBuffer.Get(otherPosition++); + } + + return equalSoFar; + } + + public abstract long Get(); + + public virtual Int64Buffer Get(long[] dest) + { + return Get(dest, 0, dest.Length); + } + + public virtual Int64Buffer Get(long[] dest, int off, int len) + { + int length = dest.Length; + if (off < 0 || len < 0 || (long)len + (long)off > length) + { + throw new IndexOutOfRangeException(); + } + + if (len > Remaining) + { + throw new BufferUnderflowException(); + } + for (int i = off; i < off + len; i++) + { + dest[i] = Get(); + } + return this; + } + + public abstract long Get(int index); + + public bool HasArray + { + get { return ProtectedHasArray; } + } + + public override int GetHashCode() + { + int myPosition = position; + int hash = 0; + long l; + while (myPosition < limit) + { + l = Get(myPosition++); + hash = hash + ((int)l) ^ ((int)(l >> 32)); + } + return hash; + } + + public abstract bool IsDirect { get; } + + public abstract ByteOrder Order { get; } + + [WritableArray] + [SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")] + protected abstract long[] ProtectedArray { get; } + + protected abstract int ProtectedArrayOffset { get; } + + protected abstract bool ProtectedHasArray { get; } + + public abstract Int64Buffer Put(long l); + + public Int64Buffer Put(long[] src) + { + return Put(src, 0, src.Length); + } + + public virtual Int64Buffer Put(long[] src, int off, int len) + { + int length = src.Length; + if (off < 0 || len < 0 || (long)len + (long)off > length) + { + throw new IndexOutOfRangeException(); + } + + if (len > Remaining) + { + throw new BufferOverflowException(); + } + for (int i = off; i < off + len; i++) + { + Put(src[i]); + } + return this; + } + + public virtual Int64Buffer Put(Int64Buffer src) + { + if (src == this) + { + throw new ArgumentException(); + } + if (src.Remaining > Remaining) + { + throw new BufferOverflowException(); + } + long[] contents = new long[src.Remaining]; + src.Get(contents); + Put(contents); + return this; + } + + public abstract Int64Buffer Put(int index, long l); + + public abstract Int64Buffer Slice(); + + public override string ToString() + { + StringBuilder buf = new StringBuilder(); + buf.Append(GetType().GetTypeInfo().Name); + buf.Append(", status: capacity="); //$NON-NLS-1$ + buf.Append(Capacity); + buf.Append(" position="); //$NON-NLS-1$ + buf.Append(Position); + buf.Append(" limit="); //$NON-NLS-1$ + buf.Append(Limit); + return buf.ToString(); + } + } +} + + + +///* +// * 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.IO +//{ +// /// <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 bool HasArray +// { +// get +// { +// return (hb != null) && !isReadOnly; +// } +// } + +// public object Array +// { +// get +// { +// if (hb == null) +// throw new InvalidOperationException(); +// if (isReadOnly) +// throw new ReadOnlyBufferException(); +// return hb; +// } +// } + +// public 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 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/IO/LongToByteBufferAdapter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/LongToByteBufferAdapter.cs b/src/Lucene.Net/Support/IO/LongToByteBufferAdapter.cs new file mode 100644 index 0000000..49e9862 --- /dev/null +++ b/src/Lucene.Net/Support/IO/LongToByteBufferAdapter.cs @@ -0,0 +1,233 @@ +// This class was sourced from the Apache Harmony project + +using System; +using System.Diagnostics.CodeAnalysis; + +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> + /// This class wraps a byte buffer to be a long buffer. + /// <para/> + /// Implementation notice: + /// <list type="bullet"> + /// <item>After a byte buffer instance is wrapped, it becomes privately owned by + /// the adapter. It must NOT be accessed outside the adapter any more.</item> + /// <item>The byte buffer's position and limit are NOT linked with the adapter. + /// The adapter extends Buffer, thus has its own position and limit.</item> + /// </list> + /// </summary> + internal sealed class Int64ToByteBufferAdapter : Int64Buffer + { + internal static Int64Buffer Wrap(ByteBuffer byteBuffer) + { + return new Int64ToByteBufferAdapter(byteBuffer.Slice()); + } + + private readonly ByteBuffer byteBuffer; + + internal Int64ToByteBufferAdapter(ByteBuffer byteBuffer) + : base((byteBuffer.Capacity >> 3)) + { + this.byteBuffer = byteBuffer; + this.byteBuffer.Clear(); + } + + //public int GetByteCapacity() + //{ + // if (byteBuffer is DirectBuffer) { + // return ((DirectBuffer)byteBuffer).GetByteCapacity(); + // } + // return -1; + //} + + //public PlatformAddress getEffectiveAddress() + //{ + // if (byteBuffer is DirectBuffer) { + // return ((DirectBuffer)byteBuffer).getEffectiveAddress(); + // } + // assert false : byteBuffer; + // return null; + //} + + //public PlatformAddress getBaseAddress() + //{ + // if (byteBuffer instanceof DirectBuffer) { + // return ((DirectBuffer)byteBuffer).getBaseAddress(); + // } + // assert false : byteBuffer; + // return null; + //} + + //public boolean isAddressValid() + //{ + // if (byteBuffer instanceof DirectBuffer) { + // return ((DirectBuffer)byteBuffer).isAddressValid(); + // } + // assert false : byteBuffer; + // return false; + //} + + //public void addressValidityCheck() + //{ + // if (byteBuffer instanceof DirectBuffer) { + // ((DirectBuffer)byteBuffer).addressValidityCheck(); + // } else { + // assert false : byteBuffer; + // } + //} + + //public void free() + //{ + // if (byteBuffer instanceof DirectBuffer) { + // ((DirectBuffer)byteBuffer).free(); + // } else { + // assert false : byteBuffer; + // } + //} + + public override Int64Buffer AsReadOnlyBuffer() + { + throw new NotImplementedException(); + //Int64ToByteBufferAdapter buf = new Int64ToByteBufferAdapter(byteBuffer + // .AsReadOnlyBuffer()); + //buf.limit = limit; + //buf.position = position; + //buf.mark = mark; + //return buf; + } + + public override Int64Buffer Compact() + { + if (byteBuffer.IsReadOnly) + { + throw new ReadOnlyBufferException(); + } + byteBuffer.SetLimit(limit << 3); + byteBuffer.SetPosition(position << 3); + byteBuffer.Compact(); + byteBuffer.Clear(); + position = limit - position; + limit = capacity; + mark = UNSET_MARK; + return this; + } + + + public override Int64Buffer Duplicate() + { + Int64ToByteBufferAdapter buf = new Int64ToByteBufferAdapter(byteBuffer + .Duplicate()); + buf.limit = limit; + buf.position = position; + buf.mark = mark; + return buf; + } + + + public override long Get() + { + if (position == limit) + { + throw new BufferUnderflowException(); + } + return byteBuffer.GetInt64(position++ << 3); + } + + + public override long Get(int index) + { + if (index < 0 || index >= limit) + { + throw new IndexOutOfRangeException(); + } + return byteBuffer.GetInt64(index << 3); + } + + + public override bool IsDirect + { + get { return byteBuffer.IsDirect; } + } + + + public override bool IsReadOnly + { + get { return byteBuffer.IsReadOnly; } + } + + + public override ByteOrder Order + { + get { return byteBuffer.Order; } + } + + + [WritableArray] + [SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")] + protected override long[] ProtectedArray + { + get { throw new NotSupportedException(); } + } + + + protected override int ProtectedArrayOffset + { + get { throw new NotSupportedException(); } + } + + + protected override bool ProtectedHasArray + { + get { return false; } + } + + + public override Int64Buffer Put(long c) + { + if (position == limit) + { + throw new BufferOverflowException(); + } + byteBuffer.PutInt64(position++ << 3, c); + return this; + } + + + public override Int64Buffer Put(int index, long c) + { + if (index < 0 || index >= limit) + { + throw new IndexOutOfRangeException(); + } + byteBuffer.PutInt64(index << 3, c); + return this; + } + + + public override Int64Buffer Slice() + { + byteBuffer.SetLimit(limit << 3); + byteBuffer.SetPosition(position << 3); + Int64Buffer result = new Int64ToByteBufferAdapter(byteBuffer.Slice()); + byteBuffer.Clear(); + return result; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/38d3a3d5/src/Lucene.Net/Support/IO/MemoryMappedFileByteBuffer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/MemoryMappedFileByteBuffer.cs b/src/Lucene.Net/Support/IO/MemoryMappedFileByteBuffer.cs new file mode 100644 index 0000000..2c48af6 --- /dev/null +++ b/src/Lucene.Net/Support/IO/MemoryMappedFileByteBuffer.cs @@ -0,0 +1,999 @@ +using System; +using System.IO.MemoryMappedFiles; + +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. + */ + + internal sealed class MemoryMappedFileByteBuffer : ByteBuffer, IDisposable + { + private MemoryMappedViewAccessor accessor; + private readonly int offset; + private bool bigEndian = true; //!BitConverter.IsLittleEndian; + + public MemoryMappedFileByteBuffer(MemoryMappedViewAccessor accessor, int capacity) + : base(capacity) + { + this.accessor = accessor; + } + + public MemoryMappedFileByteBuffer(MemoryMappedViewAccessor accessor, int capacity, int offset) + : this(accessor, capacity) + { + this.offset = offset; + } + + //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() + { + var buffer = new MemoryMappedFileByteBuffer(accessor, Remaining, offset + position); + buffer.order = this.order; + return buffer; + } + + public override ByteBuffer Duplicate() + { + var buffer = new MemoryMappedFileByteBuffer(accessor, Capacity, offset); + buffer.mark = this.mark; + buffer.position = this.Position; + buffer.limit = this.Limit; + return buffer; + } + + 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) + { + if ((offset | length | (offset + length) | (dst.Length - (offset + length))) < 0) + { + throw new IndexOutOfRangeException(); + } + 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) + { + if ((offset | length | (offset + length) | (src.Length - (offset + length))) < 0) + { + throw new IndexOutOfRangeException(); + } + 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(); + } + + + 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(); + } + + protected override byte[] ProtectedArray + { + get { throw new NotSupportedException(); } + } + + protected override int ProtectedArrayOffset + { + get { throw new NotSupportedException(); } + } + + protected override bool ProtectedHasArray + { + get { return false; } + } + + + /// <summary> + /// Checks the current position against the limit, throwing a + /// <see cref="BufferUnderflowException"/> if it is not smaller than the limit, and then + /// increments the position. + /// </summary> + /// <returns>The current position value, before it is incremented</returns> + internal int NextGetIndex() + { + if (position >= limit) + { + throw new BufferUnderflowException(); + } + int p = position++; + UnsetMarkIfNecessary(); + return p; + } + + internal int NextGetIndex(int nb) + { + if (limit - position < nb) + { + throw new BufferUnderflowException(); + } + int p = position; + position += nb; + UnsetMarkIfNecessary(); + return p; + } + + /// <summary> + /// Checks the current position against the limit, throwing a <see cref="BufferOverflowException"/> + /// if it is not smaller than the limit, and then + /// increments the position. + /// </summary> + /// <returns>The current position value, before it is incremented</returns> + internal int NextPutIndex() + { + if (position >= limit) + { + throw new BufferOverflowException(); + } + int p = position++; + UnsetMarkIfNecessary(); + return p; + } + + internal int NextPutIndex(int nb) + { + if (limit - position < nb) + { + throw new BufferOverflowException(); + } + int p = position; + position += nb; + UnsetMarkIfNecessary(); + return p; + } + + internal void UnsetMarkIfNecessary() + { + if ((mark != UNSET_MARK) && (mark > position)) + { + mark = UNSET_MARK; + } + } + + /// <summary> + /// Checks the given index against the limit, throwing an <see cref="IndexOutOfRangeException"/> + /// if it is not smaller than the limit or is smaller than zero. + /// </summary> + /// <param name="i"></param> + /// <returns></returns> + internal int CheckIndex(int i) + { + if ((i < 0) || (i >= limit)) + { + throw new IndexOutOfRangeException(); + } + return i; + } + + internal int CheckIndex(int i, int nb) + { + if ((i < 0) || (nb > limit - i)) + { + throw new IndexOutOfRangeException(); + } + return i; + } + } +} + + + +//using Lucene.Net.Support.IO; +//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
