http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/DataInputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/DataInputStream.cs b/src/Lucene.Net/Support/DataInputStream.cs deleted file mode 100644 index 6558e87..0000000 --- a/src/Lucene.Net/Support/DataInputStream.cs +++ /dev/null @@ -1,413 +0,0 @@ -/* - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -using System; -using System.IO; - -namespace Lucene.Net.Support -{ - /// <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(IDataInput)"/> method. - /// </para> - /// </summary> - public class DataInputStream : IDataInput, IDisposable - { - private readonly Stream @in; - - /// <summary> - /// Creates a DataInputStream that uses the specified - /// underlying InputStream. - /// </summary> - /// <param name="in">the specified input stream</param> - public DataInputStream(Stream @in) - { - this.@in = @in; - } - - /// <summary> - /// working arrays initialized on demand by readUTF - /// </summary> - private byte[] bytearr = new byte[80]; - private char[] chararr = new char[80]; - - public int Read(byte[] b) - { - return @in.Read(b, 0, b.Length); - } - - public int Read(byte[] b, int off, int len) - { - return @in.Read(b, off, len); - } - - public void ReadFully(byte[] b) - { - ReadFully(b, 0, b.Length); - } - - public void ReadFully(byte[] b, int off, int len) - { - if (len < 0) - { - throw new IndexOutOfRangeException(); - } - int n = 0; - while (n < len) - { - int count = @in.Read(b, off + n, len - n); - if (count == 0) - { - throw new EndOfStreamException(); - } - n += count; - } - } - - public int SkipBytes(int n) - { - int total = 0; - int cur = 0; - - while ((total < n) && ((cur = Skip(@in, n - total)) > 0)) - { - total += cur; - } - - return total; - } - - /// <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 bool ReadBoolean() - { - int ch = @in.ReadByte(); - if (ch < 0) - { - throw new EndOfStreamException(); - } - return (ch != 0); - } - - /// <summary> - /// NOTE: This was readByte() in the JDK - /// </summary> - public int ReadSByte() - { - int ch = @in.ReadByte(); - if (ch < 0) - { - throw new EndOfStreamException(); - } - return ch; - } - - /// <summary> - /// NOTE: This was readUnsignedByte() in the JDK - /// </summary> - public byte ReadByte() - { - int ch = @in.ReadByte(); - if (ch < 0) - { - throw new EndOfStreamException(); - } - return (byte)ch; - } - - /// <summary> - /// NOTE: This was readShort() in the JDK - /// </summary> - public short ReadInt16() - { - int ch1 = @in.ReadByte(); - int ch2 = @in.ReadByte(); - if ((ch1 | ch2) < 0) - { - throw new EndOfStreamException(); - } - return (short)((ch1 << 8) + (ch2 << 0)); - } - - /// <summary> - /// NOTE: This was readUnsignedShort() in the JDK - /// </summary> - public int ReadUInt16() - { - int ch1 = @in.ReadByte(); - int ch2 = @in.ReadByte(); - if ((ch1 | ch2) < 0) - { - throw new EndOfStreamException(); - } - return (ch1 << 8) + (ch2 << 0); - } - - public char ReadChar() - { - int ch1 = @in.ReadByte(); - int ch2 = @in.ReadByte(); - if ((ch1 | ch2) < 0) - { - throw new EndOfStreamException(); - } - return (char)((ch1 << 8) + (ch2 << 0)); - } - - /// <summary> - /// NOTE: This was readInt() in the JDK - /// </summary> - public int ReadInt32() - { - int ch1 = @in.ReadByte(); - int ch2 = @in.ReadByte(); - int ch3 = @in.ReadByte(); - int ch4 = @in.ReadByte(); - if ((ch1 | ch2 | ch3 | ch4) < 0) - { - throw new EndOfStreamException(); - } - return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); - } - - private byte[] readBuffer = new byte[8]; - - /// <summary> - /// NOTE: This was readLong() in the JDK - /// </summary> - public long ReadInt64() - { - ReadFully(readBuffer, 0, 8); - return (((long)readBuffer[0] << 56) + - ((long)(readBuffer[1] & 255) << 48) + - ((long)(readBuffer[2] & 255) << 40) + - ((long)(readBuffer[3] & 255) << 32) + - ((long)(readBuffer[4] & 255) << 24) + - ((readBuffer[5] & 255) << 16) + - ((readBuffer[6] & 255) << 8) + - ((readBuffer[7] & 255) << 0)); - } - - /// <summary> - /// NOTE: This was readFloat() in the JDK - /// </summary> - public float ReadSingle() - { - return Number.Int32BitsToSingle(ReadInt32()); - } - - public double ReadDouble() - { - throw new NotImplementedException(); - //return Number.LongBitsToDouble(ReadLong()); - } - - private char[] lineBuffer; - - [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); - } - - public string ReadUTF() - { - return ReadUTF(this); - } - - public static string ReadUTF(IDataInput @in) - { - int utflen = @in.ReadUInt16(); - byte[] bytearr = null; - char[] chararr = null; - if (@in is DataInputStream) - { - DataInputStream dis = (DataInputStream)@in; - if (dis.bytearr.Length < utflen) - { - dis.bytearr = new byte[utflen * 2]; - dis.chararr = new char[utflen * 2]; - } - chararr = dis.chararr; - bytearr = dis.bytearr; - } - else - { - bytearr = new byte[utflen]; - chararr = new char[utflen]; - } - - int c, char2, char3; - int count = 0; - int chararr_count = 0; - - @in.ReadFully(bytearr, 0, utflen); - - while (count < utflen) - { - c = (int)bytearr[count] & 0xff; - if (c > 127) break; - count++; - chararr[chararr_count++] = (char)c; - } - - while (count < utflen) - { - c = (int)bytearr[count] & 0xff; - switch (c >> 4) - { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx*/ - count++; - chararr[chararr_count++] = (char)c; - break; - case 12: - case 13: - /* 110x xxxx 10xx xxxx*/ - count += 2; - if (count > utflen) - { - throw new FormatException( - "malformed input: partial character at end"); - } - char2 = (int)bytearr[count - 1]; - if ((char2 & 0xC0) != 0x80) - { - throw new FormatException( - "malformed input around byte " + count); - } - chararr[chararr_count++] = (char)(((c & 0x1F) << 6) | - (char2 & 0x3F)); - break; - case 14: - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - count += 3; - if (count > utflen) - { - throw new FormatException( - "malformed input: partial character at end"); - } - char2 = (int)bytearr[count - 2]; - char3 = (int)bytearr[count - 1]; - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - { - throw new FormatException( - "malformed input around byte " + (count - 1)); - } - chararr[chararr_count++] = (char)(((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - ((char3 & 0x3F) << 0)); - break; - default: - /* 10xx xxxx, 1111 xxxx */ - throw new FormatException( - "malformed input around byte " + count); - } - } - // The number of chars produced may be less than utflen - return new string(chararr, 0, chararr_count); - } - - public void Dispose() - { - @in.Dispose(); - } - } -}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/DataOutputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/DataOutputStream.cs b/src/Lucene.Net/Support/DataOutputStream.cs deleted file mode 100644 index 42fcf69..0000000 --- a/src/Lucene.Net/Support/DataOutputStream.cs +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -using System; -using System.IO; -using System.Runtime.CompilerServices; - -namespace Lucene.Net.Support -{ - /// <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(IDataOutput)"/> method. - /// </para> - /// </summary> - public class DataOutputStream : IDataOutput, IDisposable - { - private readonly object _lock = new object(); - - /// <summary> - /// The number of bytes written to the data output stream so far. - /// If this counter overflows, it will be wrapped to <see cref="int.MaxValue"/>. - /// </summary> - protected int written; - - /// <summary> - /// bytearr is initialized on demand by writeUTF - /// </summary> - private byte[] bytearr = null; - - - private readonly Stream @out; - - /// <summary> - /// Creates a new data output stream to write data to the specified - /// underlying output stream. The counter <code>written</code> is - /// set to zero. - /// </summary> - /// <param name="out">the underlying output stream, to be saved for later use.</param> - public DataOutputStream(Stream @out) - { - this.@out = @out; - } - - /// <summary> - /// Increases the written counter by the specified value - /// until it reaches <see cref="int.MaxValue"/>. - /// </summary> - private void IncCount(int value) - { - int temp = written + value; - if (temp < 0) - { - temp = int.MaxValue; - } - written = temp; - } - - /// <summary> - /// Writes the specified byte (the low eight bits of the argument - /// <code>b</code>) to the underlying output stream.If no exception - /// is thrown, the counter<code>written</code> is incremented by - /// <code>1</code>. - /// </summary> - /// <param name="b">the <code>byte</code> to be written.</param> - public virtual void Write(int b) - { - lock (_lock) - { - @out.WriteByte((byte)b); - IncCount(1); - } - } - - public virtual void Write(byte[] b, int off, int len) - { - lock (_lock) - { - @out.Write(b, off, len); - IncCount(len); - } - } - - public virtual void Flush() - { - @out.Flush(); - } - - public void WriteBoolean(bool v) - { - @out.WriteByte((byte)(v ? 1 : 0)); - IncCount(1); - } - - public void WriteByte(int v) - { - @out.WriteByte((byte)v); - IncCount(1); - } - - /// <summary> - /// NOTE: This was writeShort() in the JDK - /// </summary> - public void WriteInt16(int v) - { - @out.WriteByte((byte)((int)((uint)v >> 8) & 0xFF)); - @out.WriteByte((byte)((int)((uint)v >> 0) & 0xFF)); - IncCount(2); - } - - public void WriteChar(int v) - { - @out.WriteByte((byte)((int)((uint)v >> 8) & 0xFF)); - @out.WriteByte((byte)((int)((uint)v >> 0) & 0xFF)); - IncCount(2); - } - - /// <summary> - /// NOTE: This was writeInt() in the JDK - /// </summary> - public void WriteInt32(int v) - { - @out.WriteByte((byte)(int)(((uint)v >> 24) & 0xFF)); - @out.WriteByte((byte)(int)(((uint)v >> 16) & 0xFF)); - @out.WriteByte((byte)(int)(((uint)v >> 8) & 0xFF)); - @out.WriteByte((byte)(int)(((uint)v >> 0) & 0xFF)); - IncCount(4); - } - - private byte[] writeBuffer = new byte[8]; - - /// <summary> - /// NOTE: This was writeLong() in the JDK - /// </summary> - public void WriteInt64(long v) - { - writeBuffer[0] = (byte)(long)((ulong)v >> 56); - writeBuffer[1] = (byte)(long)((ulong)v >> 48); - writeBuffer[2] = (byte)(long)((ulong)v >> 40); - writeBuffer[3] = (byte)(long)((ulong)v >> 32); - writeBuffer[4] = (byte)(long)((ulong)v >> 24); - writeBuffer[5] = (byte)(long)((ulong)v >> 16); - writeBuffer[6] = (byte)(long)((ulong)v >> 8); - writeBuffer[7] = (byte)(long)((ulong)v >> 0); - @out.Write(writeBuffer, 0, 8); - IncCount(8); - } - - /// <summary> - /// NOTE: This was writeFloat() in the JDK - /// </summary> - public void WriteSingle(float v) - { - WriteInt32(Number.SingleToInt32Bits(v)); - } - - public void WriteDouble(double v) - { - WriteInt64(Number.DoubleToInt64Bits(v)); - } - - public void WriteBytes(string s) - { - int len = s.Length; - for (int i = 0; i < len; i++) - { - @out.WriteByte((byte)s[i]); - } - IncCount(len); - } - - public void WriteChars(string s) - { - int len = s.Length; - for (int i = 0; i < len; i++) - { - int v = s[i]; - @out.WriteByte((byte)(int)(((uint)v >> 8) & 0xFF)); - @out.WriteByte((byte)(int)(((uint)v >> 0) & 0xFF)); - } - IncCount(len * 2); - } - - public void WriteUTF(string str) - { - WriteUTF(str, this); - } - - internal static int WriteUTF(string str, IDataOutput @out) - { - int strlen = str.Length; - int utflen = 0; - int c, count = 0; - - /* use charAt instead of copying String to char array */ - for (int i = 0; i < strlen; i++) - { - c = str[i]; - if ((c >= 0x0001) && (c <= 0x007F)) - { - utflen++; - } - else if (c > 0x07FF) - { - utflen += 3; - } - else - { - utflen += 2; - } - } - - if (utflen > 65535) - throw new FormatException( - "encoded string too long: " + utflen + " bytes"); - - byte[] bytearr = null; - if (@out is DataOutputStream) { - DataOutputStream dos = (DataOutputStream)@out; - if (dos.bytearr == null || (dos.bytearr.Length < (utflen + 2))) - dos.bytearr = new byte[(utflen * 2) + 2]; - bytearr = dos.bytearr; - } else { - bytearr = new byte[utflen + 2]; - } - - bytearr[count++] = (byte)(int)(((uint)utflen >> 8) & 0xFF); - bytearr[count++] = (byte)(int)(((uint)utflen >> 0) & 0xFF); - - int i2 = 0; - for (i2 = 0; i2 < strlen; i2++) - { - c = str[i2]; - if (!((c >= 0x0001) && (c <= 0x007F))) break; - bytearr[count++] = (byte)c; - } - - for (; i2 < strlen; i2++) - { - c = str[i2]; - if ((c >= 0x0001) && (c <= 0x007F)) - { - bytearr[count++] = (byte)c; - - } - else if (c > 0x07FF) - { - bytearr[count++] = (byte)(0xE0 | ((c >> 12) & 0x0F)); - bytearr[count++] = (byte)(0x80 | ((c >> 6) & 0x3F)); - bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F)); - } - else - { - bytearr[count++] = (byte)(0xC0 | ((c >> 6) & 0x1F)); - bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F)); - } - } - @out.Write(bytearr, 0, utflen + 2); - return utflen + 2; - } - - public int Length - { - get { return written; } - } - - - #region From FilterOutputStream - - public void Write(byte[] b) - { - Write(b, 0, b.Length); - } - - public void Dispose() - { - @out.Dispose(); - } - - #endregion - } -} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/Deflater.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/Deflater.cs b/src/Lucene.Net/Support/Deflater.cs deleted file mode 100644 index 019446d..0000000 --- a/src/Lucene.Net/Support/Deflater.cs +++ /dev/null @@ -1,143 +0,0 @@ -#if !NETSTANDARD -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -using System; -using System.Linq; - -namespace Lucene.Net.Support -{ - public class Deflater - { - private delegate void SetLevelDelegate(int level); - - private delegate void SetInputDelegate(byte[] input, int offset, int count); - - private delegate void FinishDelegate(); - - private delegate bool GetIsFinishedDelegate(); - - private delegate int DeflateDelegate(byte[] output); - - private delegate void ResetDelegate(); - - private delegate bool GetIsNeedingInputDelegate(); - - private delegate int DeflateDelegate3(byte[] output, int offset, int length); - - private SetLevelDelegate setLevelMethod; - private SetInputDelegate setInputMethod; - private FinishDelegate finishMethod; - private GetIsFinishedDelegate getIsFinishedMethod; - private DeflateDelegate deflateMethod; - private ResetDelegate resetMethod; - private GetIsNeedingInputDelegate getIsNeedingInputMethod; - private DeflateDelegate3 deflate3Method; - - public const int BEST_COMPRESSION = 9; - - internal Deflater(object deflaterInstance) - { - Type type = deflaterInstance.GetType(); - - setLevelMethod = (SetLevelDelegate)Delegate.CreateDelegate( - typeof(SetLevelDelegate), - deflaterInstance, - type.GetMethod("SetLevel", new Type[] { typeof(int) })); - - setInputMethod = (SetInputDelegate)Delegate.CreateDelegate( - typeof(SetInputDelegate), - deflaterInstance, - type.GetMethod("SetInput", new Type[] { typeof(byte[]), typeof(int), typeof(int) })); - - finishMethod = (FinishDelegate)Delegate.CreateDelegate( - typeof(FinishDelegate), - deflaterInstance, - type.GetMethod("Finish", Type.EmptyTypes)); - - getIsFinishedMethod = (GetIsFinishedDelegate)Delegate.CreateDelegate( - typeof(GetIsFinishedDelegate), - deflaterInstance, - type.GetMethod("get_IsFinished", Type.EmptyTypes)); - - deflateMethod = (DeflateDelegate)Delegate.CreateDelegate( - typeof(DeflateDelegate), - deflaterInstance, - type.GetMethod("Deflate", new Type[] { typeof(byte[]) })); - - resetMethod = (ResetDelegate)Delegate.CreateDelegate( - typeof(ResetDelegate), - deflaterInstance, - type.GetMethod("Reset", Type.EmptyTypes)); - - getIsNeedingInputMethod = (GetIsNeedingInputDelegate)Delegate.CreateDelegate( - typeof(GetIsNeedingInputDelegate), - deflaterInstance, - type.GetMethod("get_IsNeedingInput", Type.EmptyTypes)); - - deflate3Method = (DeflateDelegate3)Delegate.CreateDelegate( - typeof(DeflateDelegate3), - deflaterInstance, - type.GetMethod("Deflate", new Type[] { typeof(byte[]), typeof(int), typeof(int) })); - } - - public void SetLevel(int level) - { - setLevelMethod(level); - } - - public void SetInput(byte[] input, int offset, int count) - { - setInputMethod(input, offset, count); - } - - public void Finish() - { - finishMethod(); - } - - public bool IsFinished - { - get { return getIsFinishedMethod(); } - } - - public int Deflate(byte[] output) - { - return deflateMethod(output); - } - - public int Deflate(byte[] output, int offset, int length) - { - return deflate3Method(output, offset, length); - } - - public void Reset() - { - resetMethod(); - } - - public bool NeedsInput - { - get { return getIsNeedingInputMethod(); } - } - } -} -#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/FileSupport.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/FileSupport.cs b/src/Lucene.Net/Support/FileSupport.cs deleted file mode 100644 index 220fa15..0000000 --- a/src/Lucene.Net/Support/FileSupport.cs +++ /dev/null @@ -1,232 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -using System; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; - -namespace Lucene.Net.Support -{ - /// <summary> - /// Represents the methods to support some operations over files. - /// </summary> - public class FileSupport - { - private static readonly object _lock = new object(); - - /// <summary> - /// Returns an array of abstract pathnames representing the files and directories of the specified path. - /// </summary> - /// <param name="path">The abstract pathname to list it childs.</param> - /// <returns>An array of abstract pathnames childs of the path specified or null if the path is not a directory</returns> - public static System.IO.FileInfo[] GetFiles(System.IO.FileInfo path) - { - if ((path.Attributes & FileAttributes.Directory) > 0) - { - String[] fullpathnames = Directory.GetFileSystemEntries(path.FullName); - System.IO.FileInfo[] result = new System.IO.FileInfo[fullpathnames.Length]; - for (int i = 0; i < result.Length; i++) - result[i] = new System.IO.FileInfo(fullpathnames[i]); - return result; - } - else - return null; - } - - // TODO: This filesupport thing is silly. Same goes with _TestUtil's RMDir. - // If we're removing a directory - public static System.IO.FileInfo[] GetFiles(System.IO.DirectoryInfo path) - { - return GetFiles(new FileInfo(path.FullName)); - } - - ///// <summary> - ///// Returns a list of files in a give directory. - ///// </summary> - ///// <param name="fullName">The full path name to the directory.</param> - ///// <param name="indexFileNameFilter"></param> - ///// <returns>An array containing the files.</returns> - //public static System.String[] GetLuceneIndexFiles(System.String fullName, - // Index.IndexFileNameFilter indexFileNameFilter) - //{ - // System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(fullName); - // System.Collections.ArrayList list = new System.Collections.ArrayList(); - // foreach (System.IO.FileInfo fInfo in dInfo.GetFiles()) - // { - // if (indexFileNameFilter.Accept(fInfo, fInfo.Name) == true) - // { - // list.Add(fInfo.Name); - // } - // } - // System.String[] retFiles = new System.String[list.Count]; - // list.CopyTo(retFiles); - // return retFiles; - //} - - // Disable the obsolete warning since we must use FileStream.Handle - // because Mono does not support FileSystem.SafeFileHandle at present. -#pragma warning disable 618 - - /// <summary> - /// Flushes the specified file stream. Ensures that all buffered - /// data is actually written to the file system. - /// </summary> - /// <param name="fileStream">The file stream.</param> - public static void Sync(System.IO.FileStream fileStream) - { - if (fileStream == null) - throw new ArgumentNullException("fileStream"); - - fileStream.Flush(true); - - if (OS.IsWindows) - { -#if NETSTANDARD - // Getting the SafeFileHandle property automatically flushes the - // stream: https://msdn.microsoft.com/en-us/library/system.io.filestream.safefilehandle(v=vs.110).aspx - var handle = fileStream.SafeFileHandle; -#else - if (!FlushFileBuffers(fileStream.Handle)) - throw new IOException(); -#endif - } - //else if (OS.IsUnix) - //{ - // if (fsync(fileStream.Handle) != IntPtr.Zero) - // throw new System.IO.IOException(); - //} - //else - //{ - // throw new NotImplementedException(); - //} - } - -#pragma warning restore 618 - - //[System.Runtime.InteropServices.DllImport("libc")] - //extern static IntPtr fsync(IntPtr fd); - - [System.Runtime.InteropServices.DllImport("kernel32.dll")] - extern static bool FlushFileBuffers(IntPtr hFile); - - - /// <summary> - /// Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. - /// If this method returns successfully then it is guaranteed that: - /// <list type="number"> - /// <item>The file denoted by the returned abstract pathname did not exist before this method was invoked, and</item> - /// <item>Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.</item> - /// </list> - /// This method provides only part of a temporary-file facility.To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method. - /// The prefix argument must be at least three characters long. It is recommended that the prefix be a short, meaningful string such as "hjb" or "mail". The suffix argument may be null, in which case the suffix ".tmp" will be used. - /// To create the new file, the prefix and the suffix may first be adjusted to fit the limitations of the underlying platform.If the prefix is too long then it will be truncated, but its first three characters will always be preserved.If the suffix is too long then it too will be truncated, but if it begins with a period character ('.') then the period and the first three characters following it will always be preserved.Once these adjustments have been made the name of the new file will be generated by concatenating the prefix, five or more internally-generated characters, and the suffix. - /// If the directory argument is null then the system-dependent default temporary-file directory will be used.The default temporary-file directory is specified by the system property java.io.tmpdir.On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "C:\\WINNT\\TEMP". A different value may be given to this system property when the Java virtual machine is invoked, but programmatic changes to this property are not guaranteed to have any effect upon the temporary directory used by this method. - /// - /// Ported over from the java.io.File class. Used by the Analysis.Hunspell.Directory - /// class, but this can probably be removed when that class is upgraded to a more recent - /// version of lucene, where it uses the lucene Store.Directory class to create a temporary - /// file. - /// </summary> - /// <param name="prefix">The prefix string to be used in generating the file's name; must be at least three characters long</param> - /// <param name="suffix">The suffix string to be used in generating the file's name; may be null, in which case a random suffix will be generated</param> - /// <param name="directory">The directory in which the file is to be created, or null if the default temporary-file directory is to be used</param> - /// <returns></returns> - public static FileInfo CreateTempFile(string prefix, string suffix, DirectoryInfo directory) - { - lock (_lock) - { - if (string.IsNullOrEmpty(prefix)) - throw new ArgumentNullException("prefix"); - if (prefix.Length < 3) - throw new ArgumentException("Prefix string too short"); - - // Ensure the strings passed don't contain invalid characters - char[] invalid = Path.GetInvalidPathChars(); - - if (prefix.ToCharArray().Intersect(invalid).Any()) - throw new ArgumentException(string.Format("Prefix contains invalid characters. You may not use any of '{0}'", string.Join(", ", invalid))); - if (suffix != null && suffix.ToCharArray().Intersect(invalid).Any()) - throw new ArgumentException(string.Format("Suffix contains invalid characters. You may not use any of '{0}'", string.Join(", ", invalid))); - - // If no directory supplied, create one. - if (directory == null) - { - directory = new DirectoryInfo(Path.GetTempPath()); - } - string fileName = string.Empty; - - while (true) - { - fileName = NewTempFileName(prefix, suffix, directory); - - if (File.Exists(fileName)) - { - continue; - } - - try - { - // Create the file, and close it immediately - File.WriteAllText(fileName, string.Empty, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) /* No BOM */); - break; - } - catch (IOException e) - { - // If the error was because the file exists, try again - if (File.Exists(fileName)) - { - continue; - } - - // else rethrow it - throw e; - } - } - return new FileInfo(fileName); - } - } - - /// <summary> - /// Generates a new random file name with the provided <paramref name="directory"/>, - /// <paramref name="prefix"/> and optional <see cref="suffix"/>. - /// </summary> - /// <param name="prefix">The prefix string to be used in generating the file's name</param> - /// <param name="suffix">The suffix string to be used in generating the file's name; may be null, in which case a random suffix will be generated</param> - /// <param name="directory">A <see cref="DirectoryInfo"/> object containing the temp directory path. Must not be null.</param> - /// <returns>A random file name</returns> - internal static string NewTempFileName(string prefix, string suffix, DirectoryInfo directory) - { - string randomFileName = Path.GetRandomFileName(); - - if (suffix != null) - { - randomFileName = string.Concat( - Path.GetFileNameWithoutExtension(randomFileName), - suffix.StartsWith(".", StringComparison.Ordinal) ? suffix : '.' + suffix - ); - } - - return Path.Combine(directory.FullName, string.Concat(prefix, randomFileName)); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IDataInput.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IDataInput.cs b/src/Lucene.Net/Support/IDataInput.cs deleted file mode 100644 index 323b454..0000000 --- a/src/Lucene.Net/Support/IDataInput.cs +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -namespace Lucene.Net.Support -{ - /// <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 the JDK - /// </summary> - int ReadSByte(); - - /// <summary> - /// NOTE: This was readUnsignedByte() in the JDK - /// </summary> - byte ReadByte(); - - /// <summary> - /// NOTE: This was readShort() in the JDK - /// </summary> - short ReadInt16(); - - /// <summary> - /// NOTE: This was readUnsignedShort() in the JDK - /// </summary> - int ReadUInt16(); - char ReadChar(); - - /// <summary> - /// NOTE: This was readInt() in the JDK - /// </summary> - int ReadInt32(); - - /// <summary> - /// NOTE: This was readLong() in the JDK - /// </summary> - long ReadInt64(); - float ReadSingle(); - double ReadDouble(); - string ReadLine(); - string ReadUTF(); - } -} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IDataOutput.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IDataOutput.cs b/src/Lucene.Net/Support/IDataOutput.cs deleted file mode 100644 index 4b1f662..0000000 --- a/src/Lucene.Net/Support/IDataOutput.cs +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -namespace Lucene.Net.Support -{ - /// <summary> - /// Equivalent to Java's DataOutut interface - /// </summary> - public interface IDataOutput - { - void Write(int b); - void Write(byte[] b); - void Write(byte[] b, int off, int len); - void WriteBoolean(bool v); - void WriteByte(int v); - - /// <summary> - /// NOTE: This was writeShort() in the JDK - /// </summary> - void WriteInt16(int v); - void WriteChar(int v); - - /// <summary> - /// NOTE: This was writeInt() in the JDK - /// </summary> - void WriteInt32(int v); - - /// <summary> - /// NOTE: This was writeInt64() in the JDK - /// </summary> - void WriteInt64(long v); - - /// <summary> - /// NOTE: This was writeSingle() in the JDK - /// </summary> - void WriteSingle(float v); - void WriteDouble(double v); - void WriteBytes(string s); - void WriteChars(string s); - void WriteUTF(string s); - } -} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/BinaryReaderDataInput.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/BinaryReaderDataInput.cs b/src/Lucene.Net/Support/IO/BinaryReaderDataInput.cs new file mode 100644 index 0000000..0fff4d7 --- /dev/null +++ b/src/Lucene.Net/Support/IO/BinaryReaderDataInput.cs @@ -0,0 +1,50 @@ +using Lucene.Net.Store; +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. + */ + public class BinaryReaderDataInput : DataInput, IDisposable + { + private readonly BinaryReader br; + public BinaryReaderDataInput(BinaryReader br) + { + this.br = br; + } + + public override byte ReadByte() + { + return br.ReadByte(); + } + + public override void ReadBytes(byte[] b, int offset, int len) + { + byte[] temp = br.ReadBytes(len); + for (int i = offset; i < (offset + len) && i < temp.Length; i++) + { + b[i] = temp[i]; + } + } + + public void Dispose() + { + br.Dispose(); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/BinaryWriterDataOutput.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/BinaryWriterDataOutput.cs b/src/Lucene.Net/Support/IO/BinaryWriterDataOutput.cs new file mode 100644 index 0000000..98c5b7b --- /dev/null +++ b/src/Lucene.Net/Support/IO/BinaryWriterDataOutput.cs @@ -0,0 +1,47 @@ +using System; +using System.IO; +using Lucene.Net.Store; + +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 BinaryWriterDataOutput : DataOutput, IDisposable + { + private readonly BinaryWriter bw; + + public BinaryWriterDataOutput(BinaryWriter bw) + { + this.bw = bw; + } + + public override void WriteByte(byte b) + { + bw.Write(b); + } + + public override void WriteBytes(byte[] b, int offset, int length) + { + bw.Write(b, offset, length); + } + + public void Dispose() + { + bw.Dispose(); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/ByteArrayOutputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/ByteArrayOutputStream.cs b/src/Lucene.Net/Support/IO/ByteArrayOutputStream.cs new file mode 100644 index 0000000..09122ed --- /dev/null +++ b/src/Lucene.Net/Support/IO/ByteArrayOutputStream.cs @@ -0,0 +1,40 @@ +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. + */ + + // Used to wrap Java's ByteArrayOutputStream's ToString() method, as MemoryStream uses default impl + public class ByteArrayOutputStream : MemoryStream + { + public ByteArrayOutputStream() + { + } + + public ByteArrayOutputStream(int size) + : base(size) + { + } + + public override string ToString() + { + return Encoding.UTF8.GetString(this.ToArray()); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/Compression/Deflater.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/Compression/Deflater.cs b/src/Lucene.Net/Support/IO/Compression/Deflater.cs new file mode 100644 index 0000000..df48a94 --- /dev/null +++ b/src/Lucene.Net/Support/IO/Compression/Deflater.cs @@ -0,0 +1,143 @@ +#if NET35 +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +using System; +using System.Linq; + +namespace Lucene.Net.Support +{ + public class Deflater + { + private delegate void SetLevelDelegate(int level); + + private delegate void SetInputDelegate(byte[] input, int offset, int count); + + private delegate void FinishDelegate(); + + private delegate bool GetIsFinishedDelegate(); + + private delegate int DeflateDelegate(byte[] output); + + private delegate void ResetDelegate(); + + private delegate bool GetIsNeedingInputDelegate(); + + private delegate int DeflateDelegate3(byte[] output, int offset, int length); + + private SetLevelDelegate setLevelMethod; + private SetInputDelegate setInputMethod; + private FinishDelegate finishMethod; + private GetIsFinishedDelegate getIsFinishedMethod; + private DeflateDelegate deflateMethod; + private ResetDelegate resetMethod; + private GetIsNeedingInputDelegate getIsNeedingInputMethod; + private DeflateDelegate3 deflate3Method; + + public const int BEST_COMPRESSION = 9; + + internal Deflater(object deflaterInstance) + { + Type type = deflaterInstance.GetType(); + + setLevelMethod = (SetLevelDelegate)Delegate.CreateDelegate( + typeof(SetLevelDelegate), + deflaterInstance, + type.GetMethod("SetLevel", new Type[] { typeof(int) })); + + setInputMethod = (SetInputDelegate)Delegate.CreateDelegate( + typeof(SetInputDelegate), + deflaterInstance, + type.GetMethod("SetInput", new Type[] { typeof(byte[]), typeof(int), typeof(int) })); + + finishMethod = (FinishDelegate)Delegate.CreateDelegate( + typeof(FinishDelegate), + deflaterInstance, + type.GetMethod("Finish", Type.EmptyTypes)); + + getIsFinishedMethod = (GetIsFinishedDelegate)Delegate.CreateDelegate( + typeof(GetIsFinishedDelegate), + deflaterInstance, + type.GetMethod("get_IsFinished", Type.EmptyTypes)); + + deflateMethod = (DeflateDelegate)Delegate.CreateDelegate( + typeof(DeflateDelegate), + deflaterInstance, + type.GetMethod("Deflate", new Type[] { typeof(byte[]) })); + + resetMethod = (ResetDelegate)Delegate.CreateDelegate( + typeof(ResetDelegate), + deflaterInstance, + type.GetMethod("Reset", Type.EmptyTypes)); + + getIsNeedingInputMethod = (GetIsNeedingInputDelegate)Delegate.CreateDelegate( + typeof(GetIsNeedingInputDelegate), + deflaterInstance, + type.GetMethod("get_IsNeedingInput", Type.EmptyTypes)); + + deflate3Method = (DeflateDelegate3)Delegate.CreateDelegate( + typeof(DeflateDelegate3), + deflaterInstance, + type.GetMethod("Deflate", new Type[] { typeof(byte[]), typeof(int), typeof(int) })); + } + + public void SetLevel(int level) + { + setLevelMethod(level); + } + + public void SetInput(byte[] input, int offset, int count) + { + setInputMethod(input, offset, count); + } + + public void Finish() + { + finishMethod(); + } + + public bool IsFinished + { + get { return getIsFinishedMethod(); } + } + + public int Deflate(byte[] output) + { + return deflateMethod(output); + } + + public int Deflate(byte[] output, int offset, int length) + { + return deflate3Method(output, offset, length); + } + + public void Reset() + { + resetMethod(); + } + + public bool NeedsInput + { + get { return getIsNeedingInputMethod(); } + } + } +} +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/Compression/Inflater.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/Compression/Inflater.cs b/src/Lucene.Net/Support/IO/Compression/Inflater.cs new file mode 100644 index 0000000..5060b9d --- /dev/null +++ b/src/Lucene.Net/Support/IO/Compression/Inflater.cs @@ -0,0 +1,115 @@ +#if NET35 +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +using System; +using System.Linq; + +namespace Lucene.Net.Support +{ + public class Inflater + { + private delegate void SetInputDelegate(byte[] buffer); + + private delegate bool GetIsFinishedDelegate(); + + private delegate int InflateDelegate(byte[] buffer); + + private delegate void ResetDelegate(); + + private delegate void SetInputDelegate3(byte[] buffer, int index, int count); + + private delegate int InflateDelegate3(byte[] buffer, int offset, int count); + + private SetInputDelegate setInputMethod; + private GetIsFinishedDelegate getIsFinishedMethod; + private InflateDelegate inflateMethod; + private ResetDelegate resetMethod; + private SetInputDelegate3 setInput3Method; + private InflateDelegate3 inflate3Method; + + internal Inflater(object inflaterInstance) + { + Type type = inflaterInstance.GetType(); + + setInputMethod = (SetInputDelegate)Delegate.CreateDelegate( + typeof(SetInputDelegate), + inflaterInstance, + type.GetMethod("SetInput", new Type[] { typeof(byte[]) })); + + getIsFinishedMethod = (GetIsFinishedDelegate)Delegate.CreateDelegate( + typeof(GetIsFinishedDelegate), + inflaterInstance, + type.GetMethod("get_IsFinished", Type.EmptyTypes)); + + inflateMethod = (InflateDelegate)Delegate.CreateDelegate( + typeof(InflateDelegate), + inflaterInstance, + type.GetMethod("Inflate", new Type[] { typeof(byte[]) })); + + resetMethod = (ResetDelegate)Delegate.CreateDelegate( + typeof(ResetDelegate), + inflaterInstance, + type.GetMethod("Reset", Type.EmptyTypes)); + + setInput3Method = (SetInputDelegate3)Delegate.CreateDelegate( + typeof(SetInputDelegate3), + inflaterInstance, + type.GetMethod("SetInput", new Type[] { typeof(byte[]), typeof(int), typeof(int) })); + + inflate3Method = (InflateDelegate3)Delegate.CreateDelegate( + typeof(InflateDelegate3), + inflaterInstance, + type.GetMethod("Inflate", new Type[] { typeof(byte[]), typeof(int), typeof(int) })); + } + + public void SetInput(byte[] buffer) + { + setInputMethod(buffer); + } + + public void SetInput(byte[] buffer, int index, int count) + { + setInput3Method(buffer, index, count); + } + + public bool IsFinished + { + get { return getIsFinishedMethod(); } + } + + public int Inflate(byte[] buffer) + { + return inflateMethod(buffer); + } + + public int Inflate(byte[] buffer, int offset, int count) + { + return inflate3Method(buffer, offset, count); + } + + public void Reset() + { + resetMethod(); + } + } +} +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/Compression/SharpZipLib.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/Compression/SharpZipLib.cs b/src/Lucene.Net/Support/IO/Compression/SharpZipLib.cs new file mode 100644 index 0000000..0ae3d06 --- /dev/null +++ b/src/Lucene.Net/Support/IO/Compression/SharpZipLib.cs @@ -0,0 +1,53 @@ +#if NET35 +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +using System.Reflection; + +namespace Lucene.Net.Support +{ + public class SharpZipLib + { + private static System.Reflection.Assembly asm = null; + + static SharpZipLib() + { + try + { + asm = Assembly.Load("ICSharpCode.SharpZipLib"); + } + catch { } + } + + public static Deflater CreateDeflater() + { + if (asm == null) throw new System.IO.FileNotFoundException("Can not load ICSharpCode.SharpZipLib.dll"); + return new Deflater(asm.CreateInstance("ICSharpCode.SharpZipLib.Zip.Compression.Deflater")); + } + + public static Inflater CreateInflater() + { + if (asm == null) throw new System.IO.FileNotFoundException("Can not load ICSharpCode.SharpZipLib.dll"); + return new Inflater(asm.CreateInstance("ICSharpCode.SharpZipLib.Zip.Compression.Inflater")); + } + } +} +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/DataInputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/DataInputStream.cs b/src/Lucene.Net/Support/IO/DataInputStream.cs new file mode 100644 index 0000000..b24e0e2 --- /dev/null +++ b/src/Lucene.Net/Support/IO/DataInputStream.cs @@ -0,0 +1,413 @@ +/* + * 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.IO; + +namespace Lucene.Net.Support.IO +{ + /// <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(IDataInput)"/> method. + /// </para> + /// </summary> + public class DataInputStream : IDataInput, IDisposable + { + private readonly Stream @in; + + /// <summary> + /// Creates a DataInputStream that uses the specified + /// underlying InputStream. + /// </summary> + /// <param name="in">the specified input stream</param> + public DataInputStream(Stream @in) + { + this.@in = @in; + } + + /// <summary> + /// working arrays initialized on demand by readUTF + /// </summary> + private byte[] bytearr = new byte[80]; + private char[] chararr = new char[80]; + + public int Read(byte[] b) + { + return @in.Read(b, 0, b.Length); + } + + public int Read(byte[] b, int off, int len) + { + return @in.Read(b, off, len); + } + + public void ReadFully(byte[] b) + { + ReadFully(b, 0, b.Length); + } + + public void ReadFully(byte[] b, int off, int len) + { + if (len < 0) + { + throw new IndexOutOfRangeException(); + } + int n = 0; + while (n < len) + { + int count = @in.Read(b, off + n, len - n); + if (count == 0) + { + throw new EndOfStreamException(); + } + n += count; + } + } + + public int SkipBytes(int n) + { + int total = 0; + int cur = 0; + + while ((total < n) && ((cur = Skip(@in, n - total)) > 0)) + { + total += cur; + } + + return total; + } + + /// <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 bool ReadBoolean() + { + int ch = @in.ReadByte(); + if (ch < 0) + { + throw new EndOfStreamException(); + } + return (ch != 0); + } + + /// <summary> + /// NOTE: This was readByte() in the JDK + /// </summary> + public int ReadSByte() + { + int ch = @in.ReadByte(); + if (ch < 0) + { + throw new EndOfStreamException(); + } + return ch; + } + + /// <summary> + /// NOTE: This was readUnsignedByte() in the JDK + /// </summary> + public byte ReadByte() + { + int ch = @in.ReadByte(); + if (ch < 0) + { + throw new EndOfStreamException(); + } + return (byte)ch; + } + + /// <summary> + /// NOTE: This was readShort() in the JDK + /// </summary> + public short ReadInt16() + { + int ch1 = @in.ReadByte(); + int ch2 = @in.ReadByte(); + if ((ch1 | ch2) < 0) + { + throw new EndOfStreamException(); + } + return (short)((ch1 << 8) + (ch2 << 0)); + } + + /// <summary> + /// NOTE: This was readUnsignedShort() in the JDK + /// </summary> + public int ReadUInt16() + { + int ch1 = @in.ReadByte(); + int ch2 = @in.ReadByte(); + if ((ch1 | ch2) < 0) + { + throw new EndOfStreamException(); + } + return (ch1 << 8) + (ch2 << 0); + } + + public char ReadChar() + { + int ch1 = @in.ReadByte(); + int ch2 = @in.ReadByte(); + if ((ch1 | ch2) < 0) + { + throw new EndOfStreamException(); + } + return (char)((ch1 << 8) + (ch2 << 0)); + } + + /// <summary> + /// NOTE: This was readInt() in the JDK + /// </summary> + public int ReadInt32() + { + int ch1 = @in.ReadByte(); + int ch2 = @in.ReadByte(); + int ch3 = @in.ReadByte(); + int ch4 = @in.ReadByte(); + if ((ch1 | ch2 | ch3 | ch4) < 0) + { + throw new EndOfStreamException(); + } + return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); + } + + private byte[] readBuffer = new byte[8]; + + /// <summary> + /// NOTE: This was readLong() in the JDK + /// </summary> + public long ReadInt64() + { + ReadFully(readBuffer, 0, 8); + return (((long)readBuffer[0] << 56) + + ((long)(readBuffer[1] & 255) << 48) + + ((long)(readBuffer[2] & 255) << 40) + + ((long)(readBuffer[3] & 255) << 32) + + ((long)(readBuffer[4] & 255) << 24) + + ((readBuffer[5] & 255) << 16) + + ((readBuffer[6] & 255) << 8) + + ((readBuffer[7] & 255) << 0)); + } + + /// <summary> + /// NOTE: This was readFloat() in the JDK + /// </summary> + public float ReadSingle() + { + return Number.Int32BitsToSingle(ReadInt32()); + } + + public double ReadDouble() + { + throw new NotImplementedException(); + //return Number.LongBitsToDouble(ReadLong()); + } + + private char[] lineBuffer; + + [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); + } + + public string ReadUTF() + { + return ReadUTF(this); + } + + public static string ReadUTF(IDataInput @in) + { + int utflen = @in.ReadUInt16(); + byte[] bytearr = null; + char[] chararr = null; + if (@in is DataInputStream) + { + DataInputStream dis = (DataInputStream)@in; + if (dis.bytearr.Length < utflen) + { + dis.bytearr = new byte[utflen * 2]; + dis.chararr = new char[utflen * 2]; + } + chararr = dis.chararr; + bytearr = dis.bytearr; + } + else + { + bytearr = new byte[utflen]; + chararr = new char[utflen]; + } + + int c, char2, char3; + int count = 0; + int chararr_count = 0; + + @in.ReadFully(bytearr, 0, utflen); + + while (count < utflen) + { + c = (int)bytearr[count] & 0xff; + if (c > 127) break; + count++; + chararr[chararr_count++] = (char)c; + } + + while (count < utflen) + { + c = (int)bytearr[count] & 0xff; + switch (c >> 4) + { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + /* 0xxxxxxx*/ + count++; + chararr[chararr_count++] = (char)c; + break; + case 12: + case 13: + /* 110x xxxx 10xx xxxx*/ + count += 2; + if (count > utflen) + { + throw new FormatException( + "malformed input: partial character at end"); + } + char2 = (int)bytearr[count - 1]; + if ((char2 & 0xC0) != 0x80) + { + throw new FormatException( + "malformed input around byte " + count); + } + chararr[chararr_count++] = (char)(((c & 0x1F) << 6) | + (char2 & 0x3F)); + break; + case 14: + /* 1110 xxxx 10xx xxxx 10xx xxxx */ + count += 3; + if (count > utflen) + { + throw new FormatException( + "malformed input: partial character at end"); + } + char2 = (int)bytearr[count - 2]; + char3 = (int)bytearr[count - 1]; + if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) + { + throw new FormatException( + "malformed input around byte " + (count - 1)); + } + chararr[chararr_count++] = (char)(((c & 0x0F) << 12) | + ((char2 & 0x3F) << 6) | + ((char3 & 0x3F) << 0)); + break; + default: + /* 10xx xxxx, 1111 xxxx */ + throw new FormatException( + "malformed input around byte " + count); + } + } + // The number of chars produced may be less than utflen + return new string(chararr, 0, chararr_count); + } + + public void Dispose() + { + @in.Dispose(); + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/37901853/src/Lucene.Net/Support/IO/DataOutputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/DataOutputStream.cs b/src/Lucene.Net/Support/IO/DataOutputStream.cs new file mode 100644 index 0000000..4f4c6a1 --- /dev/null +++ b/src/Lucene.Net/Support/IO/DataOutputStream.cs @@ -0,0 +1,307 @@ +/* + * 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.IO; +using System.Runtime.CompilerServices; + +namespace Lucene.Net.Support.IO +{ + /// <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(IDataOutput)"/> method. + /// </para> + /// </summary> + public class DataOutputStream : IDataOutput, IDisposable + { + private readonly object _lock = new object(); + + /// <summary> + /// The number of bytes written to the data output stream so far. + /// If this counter overflows, it will be wrapped to <see cref="int.MaxValue"/>. + /// </summary> + protected int written; + + /// <summary> + /// bytearr is initialized on demand by writeUTF + /// </summary> + private byte[] bytearr = null; + + + private readonly Stream @out; + + /// <summary> + /// Creates a new data output stream to write data to the specified + /// underlying output stream. The counter <code>written</code> is + /// set to zero. + /// </summary> + /// <param name="out">the underlying output stream, to be saved for later use.</param> + public DataOutputStream(Stream @out) + { + this.@out = @out; + } + + /// <summary> + /// Increases the written counter by the specified value + /// until it reaches <see cref="int.MaxValue"/>. + /// </summary> + private void IncCount(int value) + { + int temp = written + value; + if (temp < 0) + { + temp = int.MaxValue; + } + written = temp; + } + + /// <summary> + /// Writes the specified byte (the low eight bits of the argument + /// <code>b</code>) to the underlying output stream.If no exception + /// is thrown, the counter<code>written</code> is incremented by + /// <code>1</code>. + /// </summary> + /// <param name="b">the <code>byte</code> to be written.</param> + public virtual void Write(int b) + { + lock (_lock) + { + @out.WriteByte((byte)b); + IncCount(1); + } + } + + public virtual void Write(byte[] b, int off, int len) + { + lock (_lock) + { + @out.Write(b, off, len); + IncCount(len); + } + } + + public virtual void Flush() + { + @out.Flush(); + } + + public void WriteBoolean(bool v) + { + @out.WriteByte((byte)(v ? 1 : 0)); + IncCount(1); + } + + public void WriteByte(int v) + { + @out.WriteByte((byte)v); + IncCount(1); + } + + /// <summary> + /// NOTE: This was writeShort() in the JDK + /// </summary> + public void WriteInt16(int v) + { + @out.WriteByte((byte)((int)((uint)v >> 8) & 0xFF)); + @out.WriteByte((byte)((int)((uint)v >> 0) & 0xFF)); + IncCount(2); + } + + public void WriteChar(int v) + { + @out.WriteByte((byte)((int)((uint)v >> 8) & 0xFF)); + @out.WriteByte((byte)((int)((uint)v >> 0) & 0xFF)); + IncCount(2); + } + + /// <summary> + /// NOTE: This was writeInt() in the JDK + /// </summary> + public void WriteInt32(int v) + { + @out.WriteByte((byte)(int)(((uint)v >> 24) & 0xFF)); + @out.WriteByte((byte)(int)(((uint)v >> 16) & 0xFF)); + @out.WriteByte((byte)(int)(((uint)v >> 8) & 0xFF)); + @out.WriteByte((byte)(int)(((uint)v >> 0) & 0xFF)); + IncCount(4); + } + + private byte[] writeBuffer = new byte[8]; + + /// <summary> + /// NOTE: This was writeLong() in the JDK + /// </summary> + public void WriteInt64(long v) + { + writeBuffer[0] = (byte)(long)((ulong)v >> 56); + writeBuffer[1] = (byte)(long)((ulong)v >> 48); + writeBuffer[2] = (byte)(long)((ulong)v >> 40); + writeBuffer[3] = (byte)(long)((ulong)v >> 32); + writeBuffer[4] = (byte)(long)((ulong)v >> 24); + writeBuffer[5] = (byte)(long)((ulong)v >> 16); + writeBuffer[6] = (byte)(long)((ulong)v >> 8); + writeBuffer[7] = (byte)(long)((ulong)v >> 0); + @out.Write(writeBuffer, 0, 8); + IncCount(8); + } + + /// <summary> + /// NOTE: This was writeFloat() in the JDK + /// </summary> + public void WriteSingle(float v) + { + WriteInt32(Number.SingleToInt32Bits(v)); + } + + public void WriteDouble(double v) + { + WriteInt64(Number.DoubleToInt64Bits(v)); + } + + public void WriteBytes(string s) + { + int len = s.Length; + for (int i = 0; i < len; i++) + { + @out.WriteByte((byte)s[i]); + } + IncCount(len); + } + + public void WriteChars(string s) + { + int len = s.Length; + for (int i = 0; i < len; i++) + { + int v = s[i]; + @out.WriteByte((byte)(int)(((uint)v >> 8) & 0xFF)); + @out.WriteByte((byte)(int)(((uint)v >> 0) & 0xFF)); + } + IncCount(len * 2); + } + + public void WriteUTF(string str) + { + WriteUTF(str, this); + } + + internal static int WriteUTF(string str, IDataOutput @out) + { + int strlen = str.Length; + int utflen = 0; + int c, count = 0; + + /* use charAt instead of copying String to char array */ + for (int i = 0; i < strlen; i++) + { + c = str[i]; + if ((c >= 0x0001) && (c <= 0x007F)) + { + utflen++; + } + else if (c > 0x07FF) + { + utflen += 3; + } + else + { + utflen += 2; + } + } + + if (utflen > 65535) + throw new FormatException( + "encoded string too long: " + utflen + " bytes"); + + byte[] bytearr = null; + if (@out is DataOutputStream) { + DataOutputStream dos = (DataOutputStream)@out; + if (dos.bytearr == null || (dos.bytearr.Length < (utflen + 2))) + dos.bytearr = new byte[(utflen * 2) + 2]; + bytearr = dos.bytearr; + } else { + bytearr = new byte[utflen + 2]; + } + + bytearr[count++] = (byte)(int)(((uint)utflen >> 8) & 0xFF); + bytearr[count++] = (byte)(int)(((uint)utflen >> 0) & 0xFF); + + int i2 = 0; + for (i2 = 0; i2 < strlen; i2++) + { + c = str[i2]; + if (!((c >= 0x0001) && (c <= 0x007F))) break; + bytearr[count++] = (byte)c; + } + + for (; i2 < strlen; i2++) + { + c = str[i2]; + if ((c >= 0x0001) && (c <= 0x007F)) + { + bytearr[count++] = (byte)c; + + } + else if (c > 0x07FF) + { + bytearr[count++] = (byte)(0xE0 | ((c >> 12) & 0x0F)); + bytearr[count++] = (byte)(0x80 | ((c >> 6) & 0x3F)); + bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F)); + } + else + { + bytearr[count++] = (byte)(0xC0 | ((c >> 6) & 0x1F)); + bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F)); + } + } + @out.Write(bytearr, 0, utflen + 2); + return utflen + 2; + } + + public int Length + { + get { return written; } + } + + + #region From FilterOutputStream + + public void Write(byte[] b) + { + Write(b, 0, b.Length); + } + + public void Dispose() + { + @out.Dispose(); + } + + #endregion + } +}
