@@ -40,91 +40,9 @@ namespace Lucene.Net.Support.IO
4040 /// </summary>
4141 internal static class StreamExtensions
4242 {
43+ #if ! FEATURE_RANDOMACCESS_READ
4344 private static readonly ConditionalWeakTable < Stream , object > lockCache = new ConditionalWeakTable < Stream , object > ( ) ;
44-
45- /// <summary>
46- /// Reads a sequence of bytes from a <see cref="Stream"/> to the given <see cref="ByteBuffer"/>, starting at the given position.
47- /// The <paramref name="stream"/> must be both seekable and readable.
48- /// </summary>
49- /// <param name="stream">The stream to read.</param>
50- /// <param name="destination">The <see cref="ByteBuffer"/> to write to.</param>
51- /// <param name="position">The file position at which the transfer is to begin; must be non-negative.</param>
52- /// <returns>The number of bytes read, possibly zero.</returns>
53- /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="destination"/> is <c>null</c></exception>
54- /// <exception cref="NotSupportedException">
55- /// <paramref name="stream"/> is not readable.
56- /// <para/>
57- /// -or-
58- /// <para/>
59- /// <paramref name="stream"/> is not seekable.
60- /// </exception>
61- /// <exception cref="ArgumentOutOfRangeException">
62- /// <paramref name="position"/> is less than 0.
63- /// <para/>
64- /// -or-
65- /// <para/>
66- /// <paramref name="position"/> is greater than the <see cref="Stream.Length"/> of the stream.
67- /// </exception>
68- /// <exception cref="IOException">An I/O error occurs.</exception>
69- /// <exception cref="ObjectDisposedException"><paramref name="stream"/> has already been disposed.</exception>
70- /// <remarks>
71- /// This method is atomic when used by itself, but does not synchronize with the rest of the stream methods.
72- /// </remarks>
73- public static int Read ( this Stream stream , ByteBuffer destination , long position )
74- {
75- if ( stream is null )
76- throw new ArgumentNullException ( nameof ( stream ) ) ;
77- if ( destination is null )
78- throw new ArgumentNullException ( nameof ( destination ) ) ;
79- if ( position < 0 )
80- throw new ArgumentOutOfRangeException ( nameof ( position ) ) ;
81- if ( ! stream . CanSeek )
82- throw new NotSupportedException ( "Stream does not support seeking." ) ;
83- if ( ! stream . CanRead )
84- throw new NotSupportedException ( "Stream does not support reading." ) ;
85- if ( position > stream . Length )
86- return 0 ;
87-
88- int read = 0 ;
89- object readLock = lockCache . GetOrCreateValue ( stream ) ;
90- UninterruptableMonitor . Enter ( readLock ) ;
91- try
92- {
93- long originalPosition = stream . Position ;
94- stream . Seek ( position , SeekOrigin . Begin ) ;
95-
96- if ( destination . HasArray )
97- {
98- // If the buffer has an array, we can write to it directly and save
99- // an extra copy operation.
100-
101- // Read from the stream
102- read = stream . Read ( destination . Array , destination . Position , destination . Remaining ) ;
103- destination . Position += read ;
104- }
105- else
106- {
107- // If the buffer has no array, we must use a local buffer
108- byte [ ] buffer = new byte [ destination . Remaining ] ;
109-
110- // Read from the stream
111- read = stream . Read ( buffer , 0 , buffer . Length ) ;
112-
113- // Write to the byte buffer
114- destination . Put ( buffer , 0 , read ) ;
115- }
116-
117- // Per Java's FileChannel.Read(), we don't want to alter the position
118- // of the stream, so we return it as it was originally.
119- stream . Seek ( originalPosition , SeekOrigin . Begin ) ;
120- }
121- finally
122- {
123- UninterruptableMonitor . Exit ( readLock ) ;
124- }
125-
126- return read ;
127- }
45+ #endif
12846
12947 /// <summary>
13048 /// Reads a sequence of bytes from a <see cref="Stream"/> to the given <see cref="Span{Byte}"/>,
0 commit comments