Skip to content

Commit

Permalink
Make IOUtils.CHARSET_UTF_8 not use a BOM; remove obsolete attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Mar 13, 2024
1 parent 80673c3 commit 59db9c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/Lucene.Net.Tests/Index/TestPayloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Lucene.Net.Index.Extensions;
using Lucene.Net.Support;
using Lucene.Net.Support.Threading;
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -83,7 +84,7 @@ public virtual void TestPayloadFieldBit()
// enabled in only some documents
d.Add(NewTextField("f3", "this field has payloads in some docs", Field.Store.NO));
// only add payload data for field f2
analyzer.SetPayloadData("f2", "somedata".GetBytes(Encoding.UTF8), 0, 1);
analyzer.SetPayloadData("f2", "somedata".GetBytes(IOUtils.CHARSET_UTF_8), 0, 1);
writer.AddDocument(d);
// flush
writer.Dispose();
Expand All @@ -105,8 +106,8 @@ public virtual void TestPayloadFieldBit()
d.Add(NewTextField("f2", "this field has payloads in all docs", Field.Store.NO));
d.Add(NewTextField("f3", "this field has payloads in some docs", Field.Store.NO));
// add payload data for field f2 and f3
analyzer.SetPayloadData("f2", "somedata".GetBytes(Encoding.UTF8), 0, 1);
analyzer.SetPayloadData("f3", "somedata".GetBytes(Encoding.UTF8), 0, 3);
analyzer.SetPayloadData("f2", "somedata".GetBytes(IOUtils.CHARSET_UTF_8), 0, 1);
analyzer.SetPayloadData("f3", "somedata".GetBytes(IOUtils.CHARSET_UTF_8), 0, 3);
writer.AddDocument(d);

// force merge
Expand Down
47 changes: 25 additions & 22 deletions src/Lucene.Net/Util/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ public static class IOUtils // LUCENENET specific - made static
{
/// <summary>
/// UTF-8 <see cref="Encoding"/> instance to prevent repeated
/// <see cref="Encoding.UTF8"/> lookups </summary>
[Obsolete("Use Encoding.UTF8 instead.")]
public static readonly Encoding CHARSET_UTF_8 = Encoding.UTF8;
/// <see cref="Encoding.UTF8"/> lookups and match Java's behavior
/// with respect to a lack of a byte-order mark (BOM).
/// </summary>
public static readonly Encoding CHARSET_UTF_8 = new UTF8Encoding(
encoderShouldEmitUTF8Identifier: false,
throwOnInvalidBytes: true);

/// <summary>
/// UTF-8 charset string.
Expand All @@ -58,21 +61,21 @@ public static class IOUtils // LUCENENET specific - made static
/// <code>
/// IDisposable resource1 = null, resource2 = null, resource3 = null;
/// ExpectedException priorE = null;
/// try
/// try
/// {
/// resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may throw ExpectedException
/// ..do..stuff.. // May throw ExpectedException
/// }
/// catch (ExpectedException e)
/// }
/// catch (ExpectedException e)
/// {
/// priorE = e;
/// }
/// finally
/// }
/// finally
/// {
/// IOUtils.CloseWhileHandlingException(priorE, resource1, resource2, resource3);
/// }
/// </code>
/// </para>
/// </para>
/// </summary>
/// <param name="priorException"> <c>null</c> or an exception that will be rethrown after method completion. </param>
/// <param name="objects"> Objects to call <see cref="IDisposable.Dispose()"/> on. </param>
Expand Down Expand Up @@ -148,21 +151,21 @@ public static void CloseWhileHandlingException(IEnumerable<IDisposable> objects)
/// <code>
/// IDisposable resource1 = null, resource2 = null, resource3 = null;
/// ExpectedException priorE = null;
/// try
/// try
/// {
/// resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may throw ExpectedException
/// ..do..stuff.. // May throw ExpectedException
/// }
/// catch (ExpectedException e)
/// }
/// catch (ExpectedException e)
/// {
/// priorE = e;
/// }
/// finally
/// }
/// finally
/// {
/// IOUtils.DisposeWhileHandlingException(priorE, resource1, resource2, resource3);
/// }
/// </code>
/// </para>
/// </para>
/// </summary>
/// <param name="priorException"> <c>null</c> or an exception that will be rethrown after method completion. </param>
/// <param name="objects"> Objects to call <see cref="IDisposable.Dispose()"/> on. </param>
Expand Down Expand Up @@ -201,7 +204,7 @@ public static void DisposeWhileHandlingException(Exception priorException, param
/// Disposes all given <see cref="IDisposable"/>s, suppressing all thrown exceptions. </summary>
/// <seealso cref="DisposeWhileHandlingException(Exception, IDisposable[])"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DisposeWhileHandlingException(Exception priorException, IEnumerable<IDisposable> objects)
public static void DisposeWhileHandlingException(Exception priorException, IEnumerable<IDisposable> objects)
{
Exception th = null;

Expand Down Expand Up @@ -241,7 +244,7 @@ public static void DisposeWhileHandlingException(Exception priorException, IEnum
/// <param name="objects">
/// Objects to call <see cref="IDisposable.Dispose()"/> on </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Dispose(params IDisposable[] objects)
public static void Dispose(params IDisposable[] objects)
{
Exception th = null;

Expand Down Expand Up @@ -298,7 +301,7 @@ public static void Dispose(IEnumerable<IDisposable> objects)
/// <param name="objects">
/// Objects to call <see cref="IDisposable.Dispose()"/> on </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DisposeWhileHandlingException(params IDisposable[] objects)
public static void DisposeWhileHandlingException(params IDisposable[] objects)
{
foreach (var o in objects)
{
Expand Down Expand Up @@ -334,7 +337,7 @@ public static void DisposeWhileHandlingException(IEnumerable<IDisposable> object

/// <summary>
/// Since there's no C# equivalent of Java's Exception.AddSuppressed, we add the
/// suppressed exceptions to a data field via the
/// suppressed exceptions to a data field via the
/// <see cref="ExceptionExtensions.AddSuppressed(Exception, Exception)"/> method.
/// <para/>
/// The exceptions can be retrieved by calling <see cref="ExceptionExtensions.GetSuppressed(Exception)"/>
Expand Down Expand Up @@ -480,7 +483,7 @@ public static void Copy(FileInfo source, FileInfo target)

/// <summary>
/// Simple utilty method that takes a previously caught
/// <see cref="Exception"/> and rethrows either
/// <see cref="Exception"/> and rethrows either
/// <see cref="IOException"/> or an unchecked exception. If the
/// argument is <c>null</c> then this method does nothing.
/// </summary>
Expand Down Expand Up @@ -513,8 +516,8 @@ public static void ReThrowUnchecked(Exception th)
}
}

// LUCENENET specific: Fsync is pointless in .NET, since we are
// LUCENENET specific: Fsync is pointless in .NET, since we are
// calling FileStream.Flush(true) before the stream is disposed
// which means we never need it at the point in Java where it is called.
}
}
}

0 comments on commit 59db9c9

Please sign in to comment.