This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 4baf2d19db9b04b9e408bdaf9090195d85c47679 Author: Shad Storhaug <[email protected]> AuthorDate: Mon Dec 16 02:56:28 2019 +0700 Lucene.Net.Support.IO: Factored out IDataInput, IDataOutput, DataInputStream, DataOutputStream in favor of implementations from J2N --- .../Egothor.Stemmer/Compile.cs | 4 +- .../Egothor.Stemmer/MultiTrie.cs | 2 +- .../Egothor.Stemmer/MultiTrie2.cs | 2 +- .../Egothor.Stemmer/Row.cs | 4 +- .../Egothor.Stemmer/Trie.cs | 2 +- .../Lucene.Net.Analysis.Stempel.csproj | 4 + .../Stempel/StempelStemmer.cs | 2 +- src/Lucene.Net.Replicator/Http/HttpReplicator.cs | 2 +- .../Http/ReplicationService.cs | 4 +- .../Lucene.Net.Replicator.csproj | 1 + src/Lucene.Net.Replicator/SessionToken.cs | 2 +- .../Egothor.Stemmer/TestCompile.cs | 2 +- .../SessionTokenTest.cs | 2 +- .../Support/IO/TestDataInputStream.cs | 262 -------------- .../Support/IO/TestDataOutputStream.cs | 190 ----------- src/Lucene.Net/Support/IO/DataInputStream.cs | 380 --------------------- src/Lucene.Net/Support/IO/DataOutputStream.cs | 327 ------------------ src/Lucene.Net/Support/IO/IDataInput.cs | 68 ---- src/Lucene.Net/Support/IO/IDataOutput.cs | 68 ---- 19 files changed, 18 insertions(+), 1310 deletions(-) diff --git a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Compile.cs b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Compile.cs index 9dd95ed..521715c 100644 --- a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Compile.cs +++ b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Compile.cs @@ -1,5 +1,5 @@ -using Lucene.Net.Support; -using Lucene.Net.Support.IO; +using J2N.IO; +using Lucene.Net.Support; using System; using System.Collections.Generic; using System.IO; diff --git a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie.cs b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie.cs index 84b9704..c69c679 100644 --- a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie.cs +++ b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support.IO; +using J2N.IO; using System.Collections.Generic; using System.IO; using System.Text; diff --git a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie2.cs b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie2.cs index cdc9f7d..b987418 100644 --- a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie2.cs +++ b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/MultiTrie2.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support.IO; +using J2N.IO; using System; using System.Collections.Generic; using System.Text; diff --git a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Row.cs b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Row.cs index 250972b..39cb7d1 100644 --- a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Row.cs +++ b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Row.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support.IO; +using J2N.IO; using System.Collections.Generic; using System.IO; @@ -64,8 +64,6 @@ namespace Egothor.Stemmer /// </summary> public class Row { - // LUCENENET NOTE: This was TreeMap in Java, which allows duplicate keys, but - // SortedDictionary does not. If there are issues with Stempel, check this!! internal SortedDictionary<char, Cell> cells = new SortedDictionary<char, Cell>(); internal int uniformCnt = 0; internal int uniformSkip = 0; diff --git a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Trie.cs b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Trie.cs index b0378d1..55aa889 100644 --- a/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Trie.cs +++ b/src/Lucene.Net.Analysis.Stempel/Egothor.Stemmer/Trie.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support.IO; +using J2N.IO; using System.Collections.Generic; using System.IO; diff --git a/src/Lucene.Net.Analysis.Stempel/Lucene.Net.Analysis.Stempel.csproj b/src/Lucene.Net.Analysis.Stempel/Lucene.Net.Analysis.Stempel.csproj index 1125e00..94ddb63 100644 --- a/src/Lucene.Net.Analysis.Stempel/Lucene.Net.Analysis.Stempel.csproj +++ b/src/Lucene.Net.Analysis.Stempel/Lucene.Net.Analysis.Stempel.csproj @@ -44,6 +44,10 @@ <ProjectReference Include="..\Lucene.Net.Analysis.Common\Lucene.Net.Analysis.Common.csproj" /> </ItemGroup> + <ItemGroup> + <PackageReference Include="J2N" Version="$(J2NPackageVersion)" /> + </ItemGroup> + <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' "> <DebugType>portable</DebugType> </PropertyGroup> diff --git a/src/Lucene.Net.Analysis.Stempel/Stempel/StempelStemmer.cs b/src/Lucene.Net.Analysis.Stempel/Stempel/StempelStemmer.cs index 8ce7548..b6455ab 100644 --- a/src/Lucene.Net.Analysis.Stempel/Stempel/StempelStemmer.cs +++ b/src/Lucene.Net.Analysis.Stempel/Stempel/StempelStemmer.cs @@ -1,5 +1,5 @@ using Egothor.Stemmer; -using Lucene.Net.Support.IO; +using J2N.IO; using System.IO; using System.Text; diff --git a/src/Lucene.Net.Replicator/Http/HttpReplicator.cs b/src/Lucene.Net.Replicator/Http/HttpReplicator.cs index 860ef54..f20f602 100644 --- a/src/Lucene.Net.Replicator/Http/HttpReplicator.cs +++ b/src/Lucene.Net.Replicator/Http/HttpReplicator.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support.IO; +using J2N.IO; using System; using System.IO; using System.Net.Http; diff --git a/src/Lucene.Net.Replicator/Http/ReplicationService.cs b/src/Lucene.Net.Replicator/Http/ReplicationService.cs index b40d58d..3f09a6e 100644 --- a/src/Lucene.Net.Replicator/Http/ReplicationService.cs +++ b/src/Lucene.Net.Replicator/Http/ReplicationService.cs @@ -1,5 +1,5 @@ -using Lucene.Net.Replicator.Http.Abstractions; -using Lucene.Net.Support.IO; +using J2N.IO; +using Lucene.Net.Replicator.Http.Abstractions; using Lucene.Net.Support; using Newtonsoft.Json; using System; diff --git a/src/Lucene.Net.Replicator/Lucene.Net.Replicator.csproj b/src/Lucene.Net.Replicator/Lucene.Net.Replicator.csproj index a7e2473..d81ece1 100644 --- a/src/Lucene.Net.Replicator/Lucene.Net.Replicator.csproj +++ b/src/Lucene.Net.Replicator/Lucene.Net.Replicator.csproj @@ -41,6 +41,7 @@ </ItemGroup> <ItemGroup> + <PackageReference Include="J2N" Version="$(J2NPackageVersion)" /> <PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" /> </ItemGroup> diff --git a/src/Lucene.Net.Replicator/SessionToken.cs b/src/Lucene.Net.Replicator/SessionToken.cs index e360688..274602f 100644 --- a/src/Lucene.Net.Replicator/SessionToken.cs +++ b/src/Lucene.Net.Replicator/SessionToken.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support.IO; +using J2N.IO; using System.Collections.Generic; namespace Lucene.Net.Replicator diff --git a/src/Lucene.Net.Tests.Analysis.Stempel/Egothor.Stemmer/TestCompile.cs b/src/Lucene.Net.Tests.Analysis.Stempel/Egothor.Stemmer/TestCompile.cs index af3d42b..1bc4d4d 100644 --- a/src/Lucene.Net.Tests.Analysis.Stempel/Egothor.Stemmer/TestCompile.cs +++ b/src/Lucene.Net.Tests.Analysis.Stempel/Egothor.Stemmer/TestCompile.cs @@ -19,9 +19,9 @@ * */ +using J2N.IO; using Lucene.Net; using Lucene.Net.Support; -using Lucene.Net.Support.IO; using Lucene.Net.Util; using NUnit.Framework; using System; diff --git a/src/Lucene.Net.Tests.Replicator/SessionTokenTest.cs b/src/Lucene.Net.Tests.Replicator/SessionTokenTest.cs index 46fd22c..937d381 100644 --- a/src/Lucene.Net.Tests.Replicator/SessionTokenTest.cs +++ b/src/Lucene.Net.Tests.Replicator/SessionTokenTest.cs @@ -1,6 +1,6 @@ +using J2N.IO; using Lucene.Net.Documents; using Lucene.Net.Index; -using Lucene.Net.Support.IO; using Lucene.Net.Util; using NUnit.Framework; using System.Collections.Generic; diff --git a/src/Lucene.Net.Tests/Support/IO/TestDataInputStream.cs b/src/Lucene.Net.Tests/Support/IO/TestDataInputStream.cs deleted file mode 100644 index 199a106..0000000 --- a/src/Lucene.Net.Tests/Support/IO/TestDataInputStream.cs +++ /dev/null @@ -1,262 +0,0 @@ -using Lucene.Net.Attributes; -using Lucene.Net.Util; -using NUnit.Framework; -using System; -using System.IO; -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. - */ - - public class TestDataInputStream : LuceneTestCase - { - [Test, LuceneNetSpecific] - public void TestReadFully() - { - const string READFULLY_TEST_FILE = "ReadFully.txt"; - int fileLength; - Stream @in; - - // Read one time to measure the length of the file (it may be different - // on different operating systems because of line endings) - using (@in = GetType().getResourceAsStream(READFULLY_TEST_FILE)) - { - using (var ms = new MemoryStream()) - { - @in.CopyTo(ms); - fileLength = ms.ToArray().Length; - } - } - - // Declare the buffer one byte too large - byte[] buffer = new byte[fileLength + 1]; - @in = GetType().getResourceAsStream(READFULLY_TEST_FILE); - DataInputStream dis; - using (dis = new DataInputStream(@in)) - { - // Read once for real (to the exact length) - dis.ReadFully(buffer, 0, fileLength); - } - - // Read past the end of the stream - @in = GetType().getResourceAsStream(READFULLY_TEST_FILE); - dis = new DataInputStream(@in); - bool caughtException = false; - try - { - // Using the buffer length (that is 1 byte too many) - // should generate EndOfStreamException. - dis.ReadFully(buffer, 0, buffer.Length); - } -#pragma warning disable 168 - catch (EndOfStreamException ie) -#pragma warning restore 168 - { - caughtException = true; - } - finally - { - dis.Dispose(); - if (!caughtException) - fail("Test failed"); - } - - // Ensure we get an IndexOutOfRangeException exception when length is negative - @in = GetType().getResourceAsStream(READFULLY_TEST_FILE); - dis = new DataInputStream(@in); - caughtException = false; - try - { - dis.ReadFully(buffer, 0, -20); - } -#pragma warning disable 168 - catch (IndexOutOfRangeException ie) -#pragma warning restore 168 - { - caughtException = true; - } - finally - { - dis.Dispose(); - if (!caughtException) - fail("Test failed"); - } - } - - [Test, LuceneNetSpecific] - public void TestReadLinePushback() - { - using (MemoryStream pis = new MemoryStream("\r".GetBytes(Encoding.UTF8))) - { - DataInputStream dis = new DataInputStream(pis); - -#pragma warning disable 612, 618 - string line = dis.ReadLine(); -#pragma warning restore 612, 618 - if (line == null) - { - fail("Got null, should return empty line"); - } - - long count = pis.Length - (line.Length + 1 /*account for the newline*/); - - if (count != 0) - { - fail("Test failed: available() returns " - + count + " when the file is empty"); - } - } - } - - [Test, LuceneNetSpecific] - public void TestReadUTF() - { - for (int i = 0; i < TEST_ITERATIONS; i++) - { - try - { - WriteAndReadAString(); - } - catch (FormatException utfdfe) - { - if (utfdfe.Message == null) - fail("vague exception thrown"); - } -#pragma warning disable 168 - catch (EndOfStreamException eofe) -#pragma warning restore 168 - { - // These are rare and beyond the scope of the test - } - } - } - - - private static readonly int TEST_ITERATIONS = 1000; - - private static readonly int A_NUMBER_NEAR_65535 = 60000; - - private static readonly int MAX_CORRUPTIONS_PER_CYCLE = 3; - - private static void WriteAndReadAString() - { - // Write out a string whose UTF-8 encoding is quite possibly - // longer than 65535 bytes - int length = Random.nextInt(A_NUMBER_NEAR_65535) + 1; - MemoryStream baos = new MemoryStream(); - StringBuilder testBuffer = new StringBuilder(); - for (int i = 0; i < length; i++) - testBuffer.append((char)Random.Next()); - string testString = testBuffer.toString(); - DataOutputStream dos = new DataOutputStream(baos); - dos.WriteUTF(testString); - - // Corrupt the data to produce malformed characters - byte[] testBytes = baos.ToArray(); - int dataLength = testBytes.Length; - int corruptions = Random.nextInt(MAX_CORRUPTIONS_PER_CYCLE); - for (int i = 0; i < corruptions; i++) - { - int index = Random.nextInt(dataLength); - testBytes[index] = (byte)Random.Next(); - } - - // Pay special attention to mangling the end to produce - // partial characters at end - testBytes[dataLength - 1] = (byte)Random.Next(); - testBytes[dataLength - 2] = (byte)Random.Next(); - - // Attempt to decode the bytes back into a String - MemoryStream bais = new MemoryStream(testBytes); - DataInputStream dis = new DataInputStream(bais); - dis.ReadUTF(); - } - - [Test, LuceneNetSpecific] - public void TestSkipBytes() - { - DataInputStream dis = new DataInputStream(new MyInputStream()); - dotest(dis, 0, 11, -1, 0); - dotest(dis, 0, 11, 5, 5); - Console.WriteLine("\n***CAUTION**** - may go into an infinite loop"); - dotest(dis, 5, 11, 20, 6); - } - - - private static void dotest(DataInputStream dis, int pos, int total, - int toskip, int expected) - { - - try - { - if (VERBOSE) - { - Console.WriteLine("\n\nTotal bytes in the stream = " + total); - Console.WriteLine("Currently at position = " + pos); - Console.WriteLine("Bytes to skip = " + toskip); - Console.WriteLine("Expected result = " + expected); - } - int skipped = dis.SkipBytes(toskip); - if (VERBOSE) - { - Console.WriteLine("Actual skipped = " + skipped); - } - if (skipped != expected) - { - fail("DataInputStream.skipBytes does not return expected value"); - } - } -#pragma warning disable 168 - catch (EndOfStreamException e) -#pragma warning restore 168 - { - fail("DataInputStream.skipBytes throws unexpected EOFException"); - } -#pragma warning disable 168 - catch (IOException e) -#pragma warning restore 168 - { - Console.WriteLine("IOException is thrown - possible result"); - } - } - - internal class MyInputStream : MemoryStream - { - - private int readctr = 0; - - - public override int ReadByte() - { - - if (readctr > 10) - { - return -1; - } - else - { - readctr++; - return 0; - } - - } - - } - } -} diff --git a/src/Lucene.Net.Tests/Support/IO/TestDataOutputStream.cs b/src/Lucene.Net.Tests/Support/IO/TestDataOutputStream.cs deleted file mode 100644 index 07ec1fd..0000000 --- a/src/Lucene.Net.Tests/Support/IO/TestDataOutputStream.cs +++ /dev/null @@ -1,190 +0,0 @@ -using Lucene.Net.Attributes; -using Lucene.Net.Util; -using NUnit.Framework; -using System; -using System.Diagnostics; -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 class TestDataOutputStream : LuceneTestCase - { - [Test, LuceneNetSpecific] - public void TestCounterOverflow() - { - var output = new MemoryStream(); - CounterOverflow dataOut = new CounterOverflow(output); - - dataOut.WriteByte(1); - if (dataOut.Length < 0) - { - fail("Internal counter less than 0."); - } - } - - private class CounterOverflow : DataOutputStream - { - public CounterOverflow(Stream output) - : base(output) - { - base.written = int.MaxValue; - } - } - - [Test, LuceneNetSpecific] - public void TestWriteUTF() - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(baos); - dos.WriteUTF("Hello, World!"); // 15 - dos.Flush(); - if (baos.Length != dos.Length) - fail("Miscounted bytes in DataOutputStream."); - } - - [Test, LuceneNetSpecific] - public void TestBoundsCheck() - { - byte[] data = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 }; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DummyFilterStream dfs = new DummyFilterStream(bos); - bool caughtException = false; - - // -ve length - try - { - dfs.Write(data, 0, -5); - } -#pragma warning disable 168 - catch (ArgumentOutOfRangeException ie) -#pragma warning restore 168 - { - caughtException = true; - } - finally - { - if (!caughtException) - fail("Test failed"); - } - - // -ve offset - caughtException = false; - try - { - dfs.Write(data, -2, 5); - } -#pragma warning disable 168 - catch (ArgumentOutOfRangeException ie) -#pragma warning restore 168 - { - caughtException = true; - } - finally - { - if (!caughtException) - fail("Test failed"); - } - - // off + len > data.length - caughtException = false; - try - { - dfs.Write(data, 6, 5); - } -#pragma warning disable 168 - catch (ArgumentException ie) -#pragma warning restore 168 - { - caughtException = true; - } - finally - { - if (!caughtException) - fail("Test failed"); - } - - // null data - caughtException = false; - try - { - dfs.Write(null, 0, 5); - } -#pragma warning disable 168 - catch (ArgumentNullException re) -#pragma warning restore 168 - { - caughtException = true; - } - finally - { - if (!caughtException) - fail("Test failed"); - } - } - - private class DummyFilterStream : DataOutputStream - { - - public DummyFilterStream(Stream o) - : base(o) - { - } - - public override void Write(int val) - { - base.Write(val + 1); - } - } - - [Test, LuceneNetSpecific] - public void TestWrite() - { - IDataOutput f = new F(new Sink()); - f.Write(new byte[] { 1, 2, 3 }, 0, 3); - } - - private class F : DataOutputStream - { - - public F(Stream o) - : base(o) - { - } - - public override void Write(int b) - { - Debug.WriteLine("Ignoring write of " + b); - } - - } - - private class Sink : MemoryStream - { - - public override void WriteByte(byte b) - { - throw new Exception("DataOutputStream directly invoked" - + " Write(byte) method of underlying" - + " stream"); - } - - } - } -} diff --git a/src/Lucene.Net/Support/IO/DataInputStream.cs b/src/Lucene.Net/Support/IO/DataInputStream.cs deleted file mode 100644 index 3a7dbdf..0000000 --- a/src/Lucene.Net/Support/IO/DataInputStream.cs +++ /dev/null @@ -1,380 +0,0 @@ -// This class was sourced from the Apache Harmony project -// https://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/ - -using System; -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. - */ - - /// <summary> - /// Java's DataInputStream is similar to .NET's BinaryReader. However, it reads - /// using a modified UTF-8 format that cannot be read using BinaryReader. - /// This is a port of DataInputStream that is fully compatible with Java's DataOutputStream. - /// <para> - /// Usage Note: Always favor BinaryReader over DataInputStream unless you specifically need - /// the modified UTF-8 format and/or the <see cref="ReadUTF()"/> or <see cref="DecodeUTF(int)"/> method. - /// </para> - /// </summary> - public class DataInputStream : IDataInput, IDisposable - { - private byte[] buff; - - private readonly Stream @in; - private char[] lineBuffer; - - /// <summary> - /// Constructs a new <see cref="DataInputStream"/> on the <see cref="Stream"/> <paramref name="in"/>. All - /// reads are then filtered through this stream. Note that data read by this - /// stream is not in a human readable format and was most likely created by a - /// <see cref="DataOutputStream"/>. - /// </summary> - /// <param name="in">the source <see cref="Stream"/> the filter reads from.</param> - /// <seealso cref="DataOutputStream"/> - public DataInputStream(Stream @in) - { - this.@in = @in; - buff = new byte[8]; - } - - public int Read(byte[] buffer) - { - return @in.Read(buffer, 0, buffer.Length); - } - - public int Read(byte[] buffer, int offset, int length) - { - return @in.Read(buffer, offset, length); - } - - public bool ReadBoolean() - { - int temp = @in.ReadByte(); - if (temp < 0) - { - throw new EndOfStreamException(); - } - return (temp != 0); - } - - /// <summary> - /// NOTE: This was readByte() in Java - /// </summary> - public int ReadSByte() - { - int temp = @in.ReadByte(); - if (temp < 0) - { - throw new EndOfStreamException(); - } - return temp; - } - - private int ReadToBuff(int count) - { - int offset = 0; - - while (offset < count) - { - int bytesRead = @in.Read(buff, offset, count - offset); - if (bytesRead <= 0) return bytesRead; - offset += bytesRead; - } - return offset; - } - - public char ReadChar() - { - if (ReadToBuff(2) <= 0) - { - throw new EndOfStreamException(); - } - return (char)(((buff[0] & 0xff) << 8) | (buff[1] & 0xff)); - } - - public double ReadDouble() - { - return BitConverter.Int64BitsToDouble(ReadInt64()); - } - - /// <summary> - /// NOTE: This was readFloat() in Java - /// </summary> - public float ReadSingle() - { - return Number.Int32BitsToSingle(ReadInt32()); - } - - public void ReadFully(byte[] buffer) - { - ReadFully(buffer, 0, buffer.Length); - } - - public void ReadFully(byte[] buffer, int offset, int length) - { - if (length < 0) - { - throw new IndexOutOfRangeException(); - } - if (length == 0) - { - return; - } - if (@in == null) - { - throw new NullReferenceException("Input Stream is null"); //$NON-NLS-1$ - } - if (buffer == null) - { - throw new ArgumentNullException("buffer"); //$NON-NLS-1$ - } - if (offset < 0 || offset > buffer.Length - length) - { - throw new IndexOutOfRangeException(); - } - while (length > 0) - { - int result = @in.Read(buffer, offset, length); - if (result <= 0) - { - throw new EndOfStreamException(); - } - offset += result; - length -= result; - } - } - - /// <summary> - /// NOTE: This was readInt() in Java - /// </summary> - public int ReadInt32() - { - if (ReadToBuff(4) <= 0) - { - throw new EndOfStreamException(); - } - return ((buff[0] & 0xff) << 24) | ((buff[1] & 0xff) << 16) | - ((buff[2] & 0xff) << 8) | (buff[3] & 0xff); - } - - [Obsolete] - public string ReadLine() - { - char[] buf = lineBuffer; - - if (buf == null) - { - buf = lineBuffer = new char[128]; - } - - int room = buf.Length; - int offset = 0; - int c; - - while (true) - { - switch (c = @in.ReadByte()) - { - case -1: - case '\n': - goto loop; - - case '\r': - int c2 = @in.ReadByte(); - if ((c2 != '\n') && (c2 != -1)) - { - using (StreamReader reader = new StreamReader(@in)) - { - c2 = reader.Peek(); - } - // http://stackoverflow.com/a/8021738/181087 - //if (!(in is PushbackInputStream)) { - // this.in = new PushbackInputStream(in); - //} - //((PushbackInputStream)in).unread(c2); - } - goto loop; - - default: - if (--room < 0) - { - buf = new char[offset + 128]; - room = buf.Length - offset - 1; - System.Array.Copy(lineBuffer, 0, buf, 0, offset); - lineBuffer = buf; - } - buf[offset++] = (char)c; - break; - } - } - loop: - if ((c == -1) && (offset == 0)) - { - return null; - } - return new string(buf, 0, offset); - } - - /// <summary> - /// NOTE: This was readLong() in Java - /// </summary> - public long ReadInt64() - { - if (ReadToBuff(8) <= 0) - { - throw new EndOfStreamException(); - } - int i1 = ((buff[0] & 0xff) << 24) | ((buff[1] & 0xff) << 16) | - ((buff[2] & 0xff) << 8) | (buff[3] & 0xff); - int i2 = ((buff[4] & 0xff) << 24) | ((buff[5] & 0xff) << 16) | - ((buff[6] & 0xff) << 8) | (buff[7] & 0xff); - - return ((i1 & 0xffffffffL) << 32) | (i2 & 0xffffffffL); - } - - /// <summary> - /// NOTE: This was readShort() in Java - /// </summary> - public short ReadInt16() - { - if (ReadToBuff(2) <= 0) - { - throw new EndOfStreamException(); - } - return (short)(((buff[0] & 0xff) << 8) | (buff[1] & 0xff)); - } - - /// <summary> - /// NOTE: This was readUnsignedByte() in Java - /// </summary> - public int ReadByte() - { - int temp = @in.ReadByte(); - if (temp < 0) - { - throw new EndOfStreamException(); - } - return temp; - } - - /// <summary> - /// NOTE: This was readUnsignedShort() in Java - /// </summary> - public int ReadUInt16() - { - if (ReadToBuff(2) <= 0) - { - throw new EndOfStreamException(); - } - return (char)(((buff[0] & 0xff) << 8) | (buff[1] & 0xff)); - } - - public string ReadUTF() - { - return DecodeUTF(ReadUInt16()); - } - - private string DecodeUTF(int utfSize) - { - return DecodeUTF(utfSize, this); - } - - private static string DecodeUTF(int utfSize, IDataInput @in) - { - byte[] buf = new byte[utfSize]; - char[] @out = new char[utfSize]; - @in.ReadFully(buf, 0, utfSize); - - return ConvertUTF8WithBuf(buf, @out, 0, utfSize); - } - - private static string ConvertUTF8WithBuf(byte[] buf, char[] @out, int offset, - int utfSize) - { - int count = 0, s = 0, a; - while (count < utfSize) - { - if ((@out[s] = (char)buf[offset + count++]) < '\u0080') - s++; - else if (((a = @out[s]) & 0xe0) == 0xc0) - { - if (count >= utfSize) - throw new FormatException(string.Format("Second byte at {0} does not match UTF8 Specification", - count)); - int b = buf[count++]; - if ((b & 0xC0) != 0x80) - throw new FormatException(string.Format("Second byte at {0} does not match UTF8 Specification", - (count - 1))); - @out[s++] = (char)(((a & 0x1F) << 6) | (b & 0x3F)); - } - else if ((a & 0xf0) == 0xe0) - { - if (count + 1 >= utfSize) - throw new FormatException(string.Format("Third byte at {0} does not match UTF8 Specification", - (count + 1))); - int b = buf[count++]; - int c = buf[count++]; - if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) - throw new FormatException(string.Format("Second or third byte at {0} does not match UTF8 Specification", - (count - 2))); - @out[s++] = (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F)); - } - else - { - throw new FormatException(string.Format("Input at {0} does not match UTF8 Specification", - (count - 1))); - } - } - return new string(@out, 0, s); - } - - public int SkipBytes(int count) - { - int skipped = 0; - int skip; - while (skipped < count && (skip = Skip(@in, count - skipped)) > 0) { - skipped += skip; - } - if (skipped < 0) - { - throw new EndOfStreamException(); - } - return skipped; - } - - /// <summary> - /// Helper method for SkipBytes, since Position and Seek do not work on - /// non-seekable streams. - /// </summary> - private static int Skip(Stream stream, int n) - { - int total = 0; - while (total < n && stream.ReadByte() > -1) - { - total++; - } - return total; - } - - public void Dispose() - { - @in.Dispose(); - } - } -} diff --git a/src/Lucene.Net/Support/IO/DataOutputStream.cs b/src/Lucene.Net/Support/IO/DataOutputStream.cs deleted file mode 100644 index 325bd68..0000000 --- a/src/Lucene.Net/Support/IO/DataOutputStream.cs +++ /dev/null @@ -1,327 +0,0 @@ -// This class was sourced from the Apache Harmony project -// https://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/ - -using System; -using System.IO; -using System.Runtime.CompilerServices; - -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> - /// Java's DataOutputStream is similar to .NET's BinaryWriter. However, it writes - /// in a modified UTF-8 format that cannot be read (or duplicated) using BinaryWriter. - /// This is a port of DataOutputStream that is fully compatible with Java's DataInputStream. - /// <para> - /// Usage Note: Always favor BinaryWriter over DataOutputStream unless you specifically need - /// the modified UTF-8 format and/or the <see cref="WriteUTF(string)"/> method. - /// </para> - /// </summary> - public class DataOutputStream : IDataOutput, IDisposable - { - private readonly object _lock = new object(); - - /// <summary> - /// The number of bytes written out so far. - /// </summary> - protected int written; - private byte[] buff; - - - private readonly Stream @out; - - /// <summary> - /// Constructs a new <see cref="DataOutputStream"/> on the <see cref="Stream"/> - /// <paramref name="out"/>. Note that data written by this stream is not in a human - /// readable form but can be reconstructed by using a <see cref="DataInputStream"/> - /// on the resulting output. - /// </summary> - /// <param name="out">the target stream for writing.</param> - /// <seealso cref="DataInputStream"/> - public DataOutputStream(Stream @out) - { - this.@out = @out; - buff = new byte[8]; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - public virtual void Flush() - { - @out.Flush(); - } - - public int Length - { - get - { - if (written < 0) - { - written = int.MaxValue; - } - return written; - } - } - - public virtual void Write(byte[] buffer, int offset, int count) - { - if (buffer == null) - { - throw new ArgumentNullException("buffer"); - } - lock (_lock) - { - @out.Write(buffer, offset, count); - written += count; - } - } - - public virtual void Write(int oneByte) - { - lock (_lock) - { - @out.WriteByte((byte)oneByte); - written++; - } - } - - public void WriteBoolean(bool val) - { - lock (_lock) - { - @out.WriteByte((byte)(val ? 1 : 0)); - written++; - } - } - - public void WriteByte(int val) - { - lock (_lock) - { - @out.WriteByte((byte)val); - written++; - } - } - - public void WriteBytes(string str) - { - lock (_lock) - { - if (str.Length == 0) - { - return; - } - byte[] bytes = new byte[str.Length]; - for (int index = 0; index < str.Length; index++) - { - bytes[index] = (byte)str[index]; - } - @out.Write(bytes, 0, bytes.Length); - written += bytes.Length; - } - } - - public void WriteChar(int val) - { - lock (_lock) - { - buff[0] = (byte)(val >> 8); - buff[1] = (byte)val; - @out.Write(buff, 0, 2); - written += 2; - } - } - - public void WriteChars(string str) - { - lock (_lock) - { - byte[] newBytes = new byte[str.Length * 2]; - for (int index = 0; index < str.Length; index++) - { - int newIndex = index == 0 ? index : index * 2; - newBytes[newIndex] = (byte)(str[index] >> 8); - newBytes[newIndex + 1] = (byte)str[index]; - } - @out.Write(newBytes, 0, newBytes.Length); - written += newBytes.Length; - } - } - - public void WriteDouble(double val) - { - WriteInt64(Number.DoubleToInt64Bits(val)); - } - - /// <summary> - /// NOTE: This was writeFloat() in Java - /// </summary> - public void WriteSingle(float val) - { - WriteInt32(Number.SingleToInt32Bits(val)); - } - - /// <summary> - /// NOTE: This was writeInt() in Java - /// </summary> - public void WriteInt32(int val) - { - lock (_lock) - { - buff[0] = (byte)(val >> 24); - buff[1] = (byte)(val >> 16); - buff[2] = (byte)(val >> 8); - buff[3] = (byte)val; - @out.Write(buff, 0, 4); - written += 4; - } - } - - /// <summary> - /// NOTE: This was writeLong() in Java - /// </summary> - public void WriteInt64(long val) - { - lock (_lock) - { - buff[0] = (byte)(val >> 56); - buff[1] = (byte)(val >> 48); - buff[2] = (byte)(val >> 40); - buff[3] = (byte)(val >> 32); - buff[4] = (byte)(val >> 24); - buff[5] = (byte)(val >> 16); - buff[6] = (byte)(val >> 8); - buff[7] = (byte)val; - @out.Write(buff, 0, 8); - written += 8; - } - } - - private int WriteInt64ToBuffer(long val, - byte[] buffer, int offset) - { - buffer[offset++] = (byte)(val >> 56); - buffer[offset++] = (byte)(val >> 48); - buffer[offset++] = (byte)(val >> 40); - buffer[offset++] = (byte)(val >> 32); - buffer[offset++] = (byte)(val >> 24); - buffer[offset++] = (byte)(val >> 16); - buffer[offset++] = (byte)(val >> 8); - buffer[offset++] = (byte)val; - return offset; - } - - /// <summary> - /// NOTE: This was writeShort() in Java - /// </summary> - public void WriteInt16(int val) - { - lock (_lock) - { - buff[0] = (byte)(val >> 8); - buff[1] = (byte)val; - @out.Write(buff, 0, 2); - written += 2; - } - } - - private int WriteInt16ToBuffer(int val, - byte[] buffer, int offset) - { - buffer[offset++] = (byte)(val >> 8); - buffer[offset++] = (byte)val; - return offset; - } - - public void WriteUTF(string str) - { - long utfCount = CountUTFBytes(str); - if (utfCount > 65535) - { - throw new FormatException("data format too long"); //$NON-NLS-1$ - } - byte[] buffer = new byte[(int)utfCount + 2]; - int offset = 0; - offset = WriteInt16ToBuffer((int)utfCount, buffer, offset); - offset = WriteUTFBytesToBuffer(str, (int)utfCount, buffer, offset); - Write(buffer, 0, offset); - } - - private long CountUTFBytes(string str) - { - int utfCount = 0, length = str.Length; - for (int i = 0; i < length; i++) - { - int charValue = str[i]; - if (charValue > 0 && charValue <= 127) - { - utfCount++; - } - else if (charValue <= 2047) - { - utfCount += 2; - } - else - { - utfCount += 3; - } - } - return utfCount; - } - - private int WriteUTFBytesToBuffer(string str, long count, - byte[] buffer, int offset) - { - int length = str.Length; - for (int i = 0; i < length; i++) - { - int charValue = str[i]; - if (charValue > 0 && charValue <= 127) - { - buffer[offset++] = (byte)charValue; - } - else if (charValue <= 2047) - { - buffer[offset++] = (byte)(0xc0 | (0x1f & (charValue >> 6))); - buffer[offset++] = (byte)(0x80 | (0x3f & charValue)); - } - else - { - buffer[offset++] = (byte)(0xe0 | (0x0f & (charValue >> 12))); - buffer[offset++] = (byte)(0x80 | (0x3f & (charValue >> 6))); - buffer[offset++] = (byte)(0x80 | (0x3f & charValue)); - } - } - return offset; - } - - #region From FilterOutputStream - - public void Write(byte[] b) - { - Write(b, 0, b.Length); - } - - public void Dispose() - { - @out.Dispose(); - } - - #endregion - } -} \ No newline at end of file diff --git a/src/Lucene.Net/Support/IO/IDataInput.cs b/src/Lucene.Net/Support/IO/IDataInput.cs deleted file mode 100644 index 16768bc..0000000 --- a/src/Lucene.Net/Support/IO/IDataInput.cs +++ /dev/null @@ -1,68 +0,0 @@ -// This class was sourced from the Apache Harmony project -// https://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/ - -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> - /// Equivalent to Java's DataInput interface - /// </summary> - public interface IDataInput - { - void ReadFully(byte[] b); - void ReadFully(byte[] b, int off, int len); - int SkipBytes(int n); - bool ReadBoolean(); - - /// <summary> - /// NOTE: This was readByte() in Java - /// </summary> - int ReadSByte(); - - /// <summary> - /// NOTE: This was readUnsignedByte() in Java - /// </summary> - int ReadByte(); - - /// <summary> - /// NOTE: This was readShort() in Java - /// </summary> - short ReadInt16(); - - /// <summary> - /// NOTE: This was readUnsignedShort() in Java - /// </summary> - int ReadUInt16(); - char ReadChar(); - - /// <summary> - /// NOTE: This was readInt() in Java - /// </summary> - int ReadInt32(); - - /// <summary> - /// NOTE: This was readLong() in Java - /// </summary> - long ReadInt64(); - float ReadSingle(); - double ReadDouble(); - string ReadLine(); - string ReadUTF(); - } -} diff --git a/src/Lucene.Net/Support/IO/IDataOutput.cs b/src/Lucene.Net/Support/IO/IDataOutput.cs deleted file mode 100644 index 3d02fc7..0000000 --- a/src/Lucene.Net/Support/IO/IDataOutput.cs +++ /dev/null @@ -1,68 +0,0 @@ -// This class was sourced from the Apache Harmony project -// https://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/ - -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> - /// Equivalent to Java's DataOutut interface - /// </summary> - public interface IDataOutput - { - void Write(byte[] buffer); - - void Write(byte[] buffer, int offset, int count); - - void Write(int oneByte); - - void WriteBoolean(bool val); - - void WriteByte(int val); - - void WriteBytes(string str); - - void WriteChar(int val); - - void WriteChars(string str); - - void WriteDouble(double val); - - /// <summary> - /// NOTE: This was writeFloat() in Java - /// </summary> - void WriteSingle(float val); - - /// <summary> - /// NOTE: This was writeInt() in Java - /// </summary> - void WriteInt32(int val); - - /// <summary> - /// NOTE: This was writeInt64() in Java - /// </summary> - void WriteInt64(long val); - - /// <summary> - /// NOTE: This was writeShort() in Java - /// </summary> - void WriteInt16(int val); - - void WriteUTF(string str); - } -}
