Ported DataInput, DataOutput, DataInputStream, and DataOutputStream from Apache Harmony (since the OpenJDK has a license that is off-limits)
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/8a8b289f Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/8a8b289f Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/8a8b289f Branch: refs/heads/master Commit: 8a8b289fbc0cf1ab2721f4249a7474f55d863afb Parents: 57f6ba0 Author: Shad Storhaug <[email protected]> Authored: Fri Apr 28 18:44:34 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Mon May 1 04:45:06 2017 +0700 ---------------------------------------------------------------------- LICENSE.txt | 4 - src/Lucene.Net/Support/IO/DataInputStream.cs | 469 ++++++++++----------- src/Lucene.Net/Support/IO/DataOutputStream.cs | 372 ++++++++-------- src/Lucene.Net/Support/IO/IDataInput.cs | 57 ++- src/Lucene.Net/Support/IO/IDataOutput.cs | 88 ++-- 5 files changed, 485 insertions(+), 505 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8a8b289f/LICENSE.txt ---------------------------------------------------------------------- diff --git a/LICENSE.txt b/LICENSE.txt index e0722ab..ddbc861 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -786,10 +786,6 @@ limitations under the License. ============================= Some code in -src/Lucene.Net/Support/DataInputStream.cs -src/Lucene.Net/Support/DataOutputStream.cs -src/Lucene.Net/Support/IDataInput.cs -src/Lucene.Net/Support/IDataOutput.cs src/Lucene.Net/Support/PriorityQueue.cs was derived from the Java Development Kit (JDK) and falls under the following license: http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8a8b289f/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 index b24e0e2..8054746 100644 --- a/src/Lucene.Net/Support/IO/DataInputStream.cs +++ b/src/Lucene.Net/Support/IO/DataInputStream.cs @@ -1,33 +1,28 @@ -/* - * 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. - */ +// 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. @@ -39,209 +34,145 @@ namespace Lucene.Net.Support.IO /// </summary> public class DataInputStream : IDataInput, IDisposable { + private byte[] buff; + private readonly Stream @in; + private char[] lineBuffer; /// <summary> - /// Creates a DataInputStream that uses the specified - /// underlying InputStream. + /// 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 specified input stream</param> + /// <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]; } - /// <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) + public int Read(byte[] buffer) { - ReadFully(b, 0, b.Length); + return @in.Read(buffer, 0, buffer.Length); } - public void ReadFully(byte[] b, int off, int len) + public int Read(byte[] buffer, int offset, int length) { - 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; - } + return @in.Read(buffer, offset, length); } - public int SkipBytes(int n) + public bool ReadBoolean() { - int total = 0; - int cur = 0; - - while ((total < n) && ((cur = Skip(@in, n - total)) > 0)) + int temp = @in.ReadByte(); + if (temp < 0) { - total += cur; + throw new EndOfStreamException(); } - - return total; + return (temp != 0); } /// <summary> - /// Helper method for SkipBytes, since Position and Seek do not work on - /// non-seekable streams. + /// NOTE: This was readByte() in Java /// </summary> - private static int Skip(Stream stream, int n) + public int ReadSByte() { - int total = 0; - while (total < n && stream.ReadByte() > -1) + int temp = @in.ReadByte(); + if (temp < 0) { - total++; + throw new EndOfStreamException(); } - return total; + return temp; } - public bool ReadBoolean() + private int ReadToBuff(int count) { - int ch = @in.ReadByte(); - if (ch < 0) + int offset = 0; + + while (offset < count) { - throw new EndOfStreamException(); + int bytesRead = @in.Read(buff, offset, count - offset); + if (bytesRead <= 0) return bytesRead; + offset += bytesRead; } - return (ch != 0); + return offset; } - /// <summary> - /// NOTE: This was readByte() in the JDK - /// </summary> - public int ReadSByte() + public char ReadChar() { - int ch = @in.ReadByte(); - if (ch < 0) + if (ReadToBuff(2) <= 0) { throw new EndOfStreamException(); } - return ch; + return (char)(((buff[0] & 0xff) << 8) | (buff[1] & 0xff)); } - /// <summary> - /// NOTE: This was readUnsignedByte() in the JDK - /// </summary> - public byte ReadByte() + public double ReadDouble() { - int ch = @in.ReadByte(); - if (ch < 0) - { - throw new EndOfStreamException(); - } - return (byte)ch; + return BitConverter.Int64BitsToDouble(ReadInt64()); } /// <summary> - /// NOTE: This was readShort() in the JDK + /// NOTE: This was readFloat() in Java /// </summary> - public short ReadInt16() + public float ReadSingle() { - int ch1 = @in.ReadByte(); - int ch2 = @in.ReadByte(); - if ((ch1 | ch2) < 0) - { - throw new EndOfStreamException(); - } - return (short)((ch1 << 8) + (ch2 << 0)); + return Number.Int32BitsToSingle(ReadInt32()); } - /// <summary> - /// NOTE: This was readUnsignedShort() in the JDK - /// </summary> - public int ReadUInt16() + public void ReadFully(byte[] buffer) { - int ch1 = @in.ReadByte(); - int ch2 = @in.ReadByte(); - if ((ch1 | ch2) < 0) - { - throw new EndOfStreamException(); - } - return (ch1 << 8) + (ch2 << 0); + ReadFully(buffer, 0, buffer.Length); } - public char ReadChar() + public void ReadFully(byte[] buffer, int offset, int length) { - int ch1 = @in.ReadByte(); - int ch2 = @in.ReadByte(); - if ((ch1 | ch2) < 0) + if (length < 0) { - throw new EndOfStreamException(); + 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; } - return (char)((ch1 << 8) + (ch2 << 0)); } /// <summary> - /// NOTE: This was readInt() in the JDK + /// NOTE: This was readInt() in Java /// </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) + if (ReadToBuff(4) <= 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()); + return ((buff[0] & 0xff) << 24) | ((buff[1] & 0xff) << 16) | + ((buff[2] & 0xff) << 8) | (buff[3] & 0xff); } - public double ReadDouble() - { - throw new NotImplementedException(); - //return Number.LongBitsToDouble(ReadLong()); - } - - private char[] lineBuffer; - [Obsolete] public string ReadLine() { @@ -300,109 +231,145 @@ namespace Lucene.Net.Support.IO return new string(buf, 0, offset); } - public string ReadUTF() + /// <summary> + /// NOTE: This was readLong() in Java + /// </summary> + public long ReadInt64() { - return ReadUTF(this); + 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); } - public static string ReadUTF(IDataInput @in) + /// <summary> + /// NOTE: This was readShort() in Java + /// </summary> + public short ReadInt16() { - int utflen = @in.ReadUInt16(); - byte[] bytearr = null; - char[] chararr = null; - if (@in is DataInputStream) + if (ReadToBuff(2) <= 0) { - 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; + throw new EndOfStreamException(); } - else + 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) { - bytearr = new byte[utflen]; - chararr = new char[utflen]; + throw new EndOfStreamException(); } + return temp; + } - int c, char2, char3; - int count = 0; - int chararr_count = 0; - - @in.ReadFully(bytearr, 0, utflen); - - while (count < utflen) + /// <summary> + /// NOTE: This was readUnsignedShort() in Java + /// </summary> + public int ReadUInt16() + { + if (ReadToBuff(2) <= 0) { - c = (int)bytearr[count] & 0xff; - if (c > 127) break; - count++; - chararr[chararr_count++] = (char)c; + 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); + } - while (count < utflen) + private static string ConvertUTF8WithBuf(byte[] buf, char[] @out, int offset, + int utfSize) + { + int count = 0, s = 0, a; + while (count < utfSize) { - c = (int)bytearr[count] & 0xff; - switch (c >> 4) + if ((@out[s] = (char)buf[offset + count++]) < '\u0080') + s++; + else if (((a = @out[s]) & 0xe0) == 0xc0) { - 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); + 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))); } } - // The number of chars produced may be less than utflen - return new string(chararr, 0, chararr_count); + 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() http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8a8b289f/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 index 4f4c6a1..13515da 100644 --- a/src/Lucene.Net/Support/IO/DataOutputStream.cs +++ b/src/Lucene.Net/Support/IO/DataOutputStream.cs @@ -1,34 +1,28 @@ -/* - * 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. - */ +// 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. @@ -43,253 +37,277 @@ namespace Lucene.Net.Support.IO 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"/>. + /// The number of bytes written out so far. /// </summary> protected int written; - - /// <summary> - /// bytearr is initialized on demand by writeUTF - /// </summary> - private byte[] bytearr = null; + private byte[] buff; 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. + /// 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 underlying output stream, to be saved for later use.</param> + /// <param name="out">the target stream for writing.</param> + /// <seealso cref="DataInputStream"/> public DataOutputStream(Stream @out) { this.@out = @out; + buff = new byte[8]; } - /// <summary> - /// Increases the written counter by the specified value - /// until it reaches <see cref="int.MaxValue"/>. - /// </summary> - private void IncCount(int value) + public virtual void Flush() { - int temp = written + value; - if (temp < 0) + @out.Flush(); + } + + public int Length + { + get { - temp = int.MaxValue; + if (written < 0) + { + written = int.MaxValue; + } + return written; } - 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) + public virtual void Write(byte[] buffer, int offset, int count) { + if (buffer == null) + { + throw new ArgumentNullException("buffer"); + } lock (_lock) { - @out.WriteByte((byte)b); - IncCount(1); + @out.Write(buffer, offset, count); + written += count; } } - public virtual void Write(byte[] b, int off, int len) + public virtual void Write(int oneByte) { lock (_lock) { - @out.Write(b, off, len); - IncCount(len); + @out.WriteByte((byte)oneByte); + written++; } } - public virtual void Flush() + public void WriteBoolean(bool val) { - @out.Flush(); + lock (_lock) + { + @out.WriteByte((byte)(val ? 1 : 0)); + written++; + } } - public void WriteBoolean(bool v) + public void WriteByte(int val) { - @out.WriteByte((byte)(v ? 1 : 0)); - IncCount(1); + lock (_lock) + { + @out.WriteByte((byte)val); + written++; + } } - public void WriteByte(int v) + public void WriteBytes(string str) { - @out.WriteByte((byte)v); - IncCount(1); + 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; + } } - /// <summary> - /// NOTE: This was writeShort() in the JDK - /// </summary> - public void WriteInt16(int v) + 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) { - @out.WriteByte((byte)((int)((uint)v >> 8) & 0xFF)); - @out.WriteByte((byte)((int)((uint)v >> 0) & 0xFF)); - IncCount(2); + 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 WriteChar(int v) + public void WriteDouble(double val) { - @out.WriteByte((byte)((int)((uint)v >> 8) & 0xFF)); - @out.WriteByte((byte)((int)((uint)v >> 0) & 0xFF)); - IncCount(2); + WriteInt64(Number.DoubleToInt64Bits(val)); } /// <summary> - /// NOTE: This was writeInt() in the JDK + /// NOTE: This was writeFloat() in Java /// </summary> - public void WriteInt32(int v) + public void WriteSingle(float val) { - @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); + WriteInt32(Number.SingleToInt32Bits(val)); } - private byte[] writeBuffer = new byte[8]; - /// <summary> - /// NOTE: This was writeLong() in the JDK + /// NOTE: This was writeInt() in Java /// </summary> - public void WriteInt64(long v) + public void WriteInt32(int val) { - 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); + 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 writeFloat() in the JDK + /// NOTE: This was writeLong() in Java /// </summary> - public void WriteSingle(float v) + public void WriteInt64(long val) { - WriteInt32(Number.SingleToInt32Bits(v)); + 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; + } } - public void WriteDouble(double v) + private int WriteInt64ToBuffer(long val, + byte[] buffer, int offset) { - WriteInt64(Number.DoubleToInt64Bits(v)); + 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; } - public void WriteBytes(string s) + /// <summary> + /// NOTE: This was writeShort() in Java + /// </summary> + public void WriteInt16(int val) { - int len = s.Length; - for (int i = 0; i < len; i++) + lock (_lock) { - @out.WriteByte((byte)s[i]); + buff[0] = (byte)(val >> 8); + buff[1] = (byte)val; + @out.Write(buff, 0, 2); + written += 2; } - IncCount(len); } - public void WriteChars(string s) + private int WriteInt16ToBuffer(int val, + byte[] buffer, int offset) { - 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); + buffer[offset++] = (byte)(val >> 8); + buffer[offset++] = (byte)val; + return offset; } - public void WriteUTF(string str) + public void WriteUTF(string str) { - WriteUTF(str, this); + 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); } - internal static int WriteUTF(string str, IDataOutput @out) + private long CountUTFBytes(string str) { - 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++) + int utfCount = 0, length = str.Length; + for (int i = 0; i < length; i++) { - c = str[i]; - if ((c >= 0x0001) && (c <= 0x007F)) + int charValue = str[i]; + if (charValue > 0 && charValue <= 127) { - utflen++; + utfCount++; } - else if (c > 0x07FF) + else if (charValue <= 2047) { - utflen += 3; + utfCount += 2; } else { - utflen += 2; + utfCount += 3; } } + return utfCount; + } - 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++) + private int WriteUTFBytesToBuffer(string str, long count, + byte[] buffer, int offset) + { + int length = str.Length; + for (int i = 0; i < length; i++) { - c = str[i2]; - if ((c >= 0x0001) && (c <= 0x007F)) + int charValue = str[i]; + if (charValue > 0 && charValue <= 127) { - bytearr[count++] = (byte)c; - + buffer[offset++] = (byte)charValue; } - else if (c > 0x07FF) + else if (charValue <= 2047) { - bytearr[count++] = (byte)(0xE0 | ((c >> 12) & 0x0F)); - bytearr[count++] = (byte)(0x80 | ((c >> 6) & 0x3F)); - bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F)); + buffer[offset++] = (byte)(0xc0 | (0x1f & (charValue >> 6))); + buffer[offset++] = (byte)(0x80 | (0x3f & charValue)); } else { - bytearr[count++] = (byte)(0xC0 | ((c >> 6) & 0x1F)); - bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F)); + buffer[offset++] = (byte)(0xe0 | (0x0f & (charValue >> 12))); + buffer[offset++] = (byte)(0x80 | (0x3f & (charValue >> 6))); + buffer[offset++] = (byte)(0x80 | (0x3f & charValue)); } } - @out.Write(bytearr, 0, utflen + 2); - return utflen + 2; + return offset; } - public int Length - { - get { return written; } - } - - #region From FilterOutputStream public void Write(byte[] b) @@ -304,4 +322,4 @@ namespace Lucene.Net.Support.IO #endregion } -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8a8b289f/src/Lucene.Net/Support/IO/IDataInput.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/IDataInput.cs b/src/Lucene.Net/Support/IO/IDataInput.cs index 3511038..16768bc 100644 --- a/src/Lucene.Net/Support/IO/IDataInput.cs +++ b/src/Lucene.Net/Support/IO/IDataInput.cs @@ -1,30 +1,25 @@ -/* - * 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. - */ +// 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> @@ -36,33 +31,33 @@ namespace Lucene.Net.Support.IO bool ReadBoolean(); /// <summary> - /// NOTE: This was readByte() in the JDK + /// NOTE: This was readByte() in Java /// </summary> int ReadSByte(); /// <summary> - /// NOTE: This was readUnsignedByte() in the JDK + /// NOTE: This was readUnsignedByte() in Java /// </summary> - byte ReadByte(); + int ReadByte(); /// <summary> - /// NOTE: This was readShort() in the JDK + /// NOTE: This was readShort() in Java /// </summary> short ReadInt16(); /// <summary> - /// NOTE: This was readUnsignedShort() in the JDK + /// NOTE: This was readUnsignedShort() in Java /// </summary> int ReadUInt16(); char ReadChar(); /// <summary> - /// NOTE: This was readInt() in the JDK + /// NOTE: This was readInt() in Java /// </summary> int ReadInt32(); /// <summary> - /// NOTE: This was readLong() in the JDK + /// NOTE: This was readLong() in Java /// </summary> long ReadInt64(); float ReadSingle(); http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8a8b289f/src/Lucene.Net/Support/IO/IDataOutput.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/IO/IDataOutput.cs b/src/Lucene.Net/Support/IO/IDataOutput.cs index cd9e3af..3d02fc7 100644 --- a/src/Lucene.Net/Support/IO/IDataOutput.cs +++ b/src/Lucene.Net/Support/IO/IDataOutput.cs @@ -1,64 +1,68 @@ -/* - * 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. - */ +// 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(int b); - void Write(byte[] b); - void Write(byte[] b, int off, int len); - void WriteBoolean(bool v); - void WriteByte(int v); + 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 writeShort() in the JDK + /// NOTE: This was writeFloat() in Java /// </summary> - void WriteInt16(int v); - void WriteChar(int v); + void WriteSingle(float val); /// <summary> - /// NOTE: This was writeInt() in the JDK + /// NOTE: This was writeInt() in Java /// </summary> - void WriteInt32(int v); + void WriteInt32(int val); /// <summary> - /// NOTE: This was writeInt64() in the JDK + /// NOTE: This was writeInt64() in Java /// </summary> - void WriteInt64(long v); + void WriteInt64(long val); /// <summary> - /// NOTE: This was writeSingle() in the JDK + /// NOTE: This was writeShort() in Java /// </summary> - void WriteSingle(float v); - void WriteDouble(double v); - void WriteBytes(string s); - void WriteChars(string s); - void WriteUTF(string s); + void WriteInt16(int val); + + void WriteUTF(string str); } }
