Skip to content

Commit 689669e

Browse files
harshith-212shubhluck
authored andcommitted
Revert "HDFS-14564: Add libhdfs APIs for readFully; add readFully to ByteBufferPositionedReadable (apache#963) Contributed by Sahil Takiar."
This reverts commit 12d7d26.
1 parent dfed7ea commit 689669e

File tree

16 files changed

+372
-1471
lines changed

16 files changed

+372
-1471
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.google.common.base.Preconditions;
3434
import org.apache.hadoop.classification.InterfaceAudience;
3535
import org.apache.hadoop.classification.InterfaceStability;
36-
import org.apache.hadoop.fs.ByteBufferPositionedReadable;
3736
import org.apache.hadoop.fs.ByteBufferReadable;
3837
import org.apache.hadoop.fs.CanSetDropBehind;
3938
import org.apache.hadoop.fs.CanSetReadahead;
@@ -329,40 +328,20 @@ public int read(long position, byte[] buffer, int offset, int length)
329328
throws IOException {
330329
checkStream();
331330
try {
332-
final int n = ((PositionedReadable) in).read(position, buffer, offset,
331+
final int n = ((PositionedReadable) in).read(position, buffer, offset,
333332
length);
334333
if (n > 0) {
335334
// This operation does not change the current offset of the file
336335
decrypt(position, buffer, offset, n);
337336
}
338-
337+
339338
return n;
340339
} catch (ClassCastException e) {
341340
throw new UnsupportedOperationException("This stream does not support " +
342341
"positioned read.");
343342
}
344343
}
345-
346-
/**
347-
* Positioned readFully using {@link ByteBuffer}s. This method is thread-safe.
348-
*/
349-
@Override
350-
public void readFully(long position, final ByteBuffer buf)
351-
throws IOException {
352-
checkStream();
353-
if (!(in instanceof ByteBufferPositionedReadable)) {
354-
throw new UnsupportedOperationException(in.getClass().getCanonicalName()
355-
+ " does not support positioned reads with byte buffers.");
356-
}
357-
int bufPos = buf.position();
358-
((ByteBufferPositionedReadable) in).readFully(position, buf);
359-
final int n = buf.position() - bufPos;
360-
if (n > 0) {
361-
// This operation does not change the current offset of the file
362-
decrypt(position, buf, n, bufPos);
363-
}
364-
}
365-
344+
366345
/**
367346
* Decrypt length bytes in buffer starting at offset. Output is also put
368347
* into buffer starting at offset. It is thread-safe.
@@ -396,7 +375,7 @@ private void decrypt(long position, byte[] buffer, int offset, int length)
396375
returnDecryptor(decryptor);
397376
}
398377
}
399-
378+
400379
/** Positioned read fully. It is thread-safe */
401380
@Override
402381
public void readFully(long position, byte[] buffer, int offset, int length)
@@ -428,7 +407,7 @@ public void seek(long pos) throws IOException {
428407
checkStream();
429408
try {
430409
/*
431-
* If data of target pos in the underlying stream has already been read
410+
* If data of target pos in the underlying stream has already been read
432411
* and decrypted in outBuffer, we just need to re-position outBuffer.
433412
*/
434413
if (pos <= streamOffset && pos >= (streamOffset - outBuffer.remaining())) {
@@ -544,7 +523,7 @@ public int read(ByteBuffer buf) throws IOException {
544523
* Output is also buf and same start position.
545524
* buf.position() and buf.limit() should be unchanged after decryption.
546525
*/
547-
private void decrypt(ByteBuffer buf, int n, int start)
526+
private void decrypt(ByteBuffer buf, int n, int start)
548527
throws IOException {
549528
final int pos = buf.position();
550529
final int limit = buf.limit();
@@ -626,7 +605,7 @@ public ByteBuffer read(ByteBufferPool bufferPool, int maxLength,
626605
}
627606
return buffer;
628607
} catch (ClassCastException e) {
629-
throw new UnsupportedOperationException("This stream does not support " +
608+
throw new UnsupportedOperationException("This stream does not support " +
630609
"enhanced byte buffer access.");
631610
}
632611
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ByteBufferPositionedReadable.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FSDataInputStream.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
public class FSDataInputStream extends DataInputStream
3939
implements Seekable, PositionedReadable,
4040
ByteBufferReadable, HasFileDescriptor, CanSetDropBehind, CanSetReadahead,
41-
HasEnhancedByteBufferAccess, CanUnbuffer, StreamCapabilities,
42-
ByteBufferPositionedReadable {
41+
HasEnhancedByteBufferAccess, CanUnbuffer, StreamCapabilities {
4342
/**
4443
* Map ByteBuffers that we have handed out to readers to ByteBufferPool
4544
* objects
@@ -51,8 +50,8 @@ public class FSDataInputStream extends DataInputStream
5150
public FSDataInputStream(InputStream in) {
5251
super(in);
5352
if( !(in instanceof Seekable) || !(in instanceof PositionedReadable) ) {
54-
throw new IllegalArgumentException(in.getClass().getCanonicalName() +
55-
" is not an instance of Seekable or PositionedReadable");
53+
throw new IllegalArgumentException(
54+
"In is not an instance of Seekable or PositionedReadable");
5655
}
5756
}
5857

@@ -148,8 +147,7 @@ public int read(ByteBuffer buf) throws IOException {
148147
return ((ByteBufferReadable)in).read(buf);
149148
}
150149

151-
throw new UnsupportedOperationException("Byte-buffer read unsupported " +
152-
"by " + in.getClass().getCanonicalName());
150+
throw new UnsupportedOperationException("Byte-buffer read unsupported by input stream");
153151
}
154152

155153
@Override
@@ -169,8 +167,9 @@ public void setReadahead(Long readahead)
169167
try {
170168
((CanSetReadahead)in).setReadahead(readahead);
171169
} catch (ClassCastException e) {
172-
throw new UnsupportedOperationException(in.getClass().getCanonicalName() +
173-
" does not support setting the readahead caching strategy.");
170+
throw new UnsupportedOperationException(
171+
"this stream does not support setting the readahead " +
172+
"caching strategy.");
174173
}
175174
}
176175

@@ -247,23 +246,4 @@ public boolean hasCapability(String capability) {
247246
public String toString() {
248247
return super.toString() + ": " + in;
249248
}
250-
251-
@Override
252-
public int read(long position, ByteBuffer buf) throws IOException {
253-
if (in instanceof ByteBufferPositionedReadable) {
254-
return ((ByteBufferPositionedReadable) in).read(position, buf);
255-
}
256-
throw new UnsupportedOperationException("Byte-buffer pread unsupported " +
257-
"by " + in.getClass().getCanonicalName());
258-
}
259-
260-
@Override
261-
public void readFully(long position, ByteBuffer buf) throws IOException {
262-
if (in instanceof ByteBufferPositionedReadable) {
263-
((ByteBufferPositionedReadable) in).readFully(position, buf);
264-
} else {
265-
throw new UnsupportedOperationException("Byte-buffer pread " +
266-
"unsupported by " + in.getClass().getCanonicalName());
267-
}
268-
}
269249
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/CryptoStreamsTestBase.java

Lines changed: 31 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -316,41 +316,42 @@ private void positionedReadCheck(InputStream in, int pos) throws Exception {
316316
Assert.assertArrayEquals(readData, expectedData);
317317
}
318318

319-
/** Test read fully. */
319+
/** Test read fully */
320320
@Test(timeout=120000)
321321
public void testReadFully() throws Exception {
322322
OutputStream out = getOutputStream(defaultBufferSize);
323323
writeData(out);
324324

325-
try (InputStream in = getInputStream(defaultBufferSize)) {
326-
final int len1 = dataLen / 4;
327-
// Read len1 bytes
328-
byte[] readData = new byte[len1];
329-
readAll(in, readData, 0, len1);
330-
byte[] expectedData = new byte[len1];
331-
System.arraycopy(data, 0, expectedData, 0, len1);
332-
Assert.assertArrayEquals(readData, expectedData);
333-
334-
// Pos: 1/3 dataLen
335-
readFullyCheck(in, dataLen / 3);
336-
337-
// Read len1 bytes
338-
readData = new byte[len1];
339-
readAll(in, readData, 0, len1);
340-
expectedData = new byte[len1];
341-
System.arraycopy(data, len1, expectedData, 0, len1);
342-
Assert.assertArrayEquals(readData, expectedData);
343-
344-
// Pos: 1/2 dataLen
345-
readFullyCheck(in, dataLen / 2);
346-
347-
// Read len1 bytes
348-
readData = new byte[len1];
349-
readAll(in, readData, 0, len1);
350-
expectedData = new byte[len1];
351-
System.arraycopy(data, 2 * len1, expectedData, 0, len1);
352-
Assert.assertArrayEquals(readData, expectedData);
353-
}
325+
InputStream in = getInputStream(defaultBufferSize);
326+
final int len1 = dataLen / 4;
327+
// Read len1 bytes
328+
byte[] readData = new byte[len1];
329+
readAll(in, readData, 0, len1);
330+
byte[] expectedData = new byte[len1];
331+
System.arraycopy(data, 0, expectedData, 0, len1);
332+
Assert.assertArrayEquals(readData, expectedData);
333+
334+
// Pos: 1/3 dataLen
335+
readFullyCheck(in, dataLen / 3);
336+
337+
// Read len1 bytes
338+
readData = new byte[len1];
339+
readAll(in, readData, 0, len1);
340+
expectedData = new byte[len1];
341+
System.arraycopy(data, len1, expectedData, 0, len1);
342+
Assert.assertArrayEquals(readData, expectedData);
343+
344+
// Pos: 1/2 dataLen
345+
readFullyCheck(in, dataLen / 2);
346+
347+
// Read len1 bytes
348+
readData = new byte[len1];
349+
readAll(in, readData, 0, len1);
350+
expectedData = new byte[len1];
351+
System.arraycopy(data, 2 * len1, expectedData, 0, len1);
352+
Assert.assertArrayEquals(readData, expectedData);
353+
354+
in.close();
354355
}
355356

356357
private void readFullyCheck(InputStream in, int pos) throws Exception {
@@ -368,60 +369,6 @@ private void readFullyCheck(InputStream in, int pos) throws Exception {
368369
} catch (EOFException e) {
369370
}
370371
}
371-
372-
/** Test byte byffer read fully. */
373-
@Test(timeout=120000)
374-
public void testByteBufferReadFully() throws Exception {
375-
OutputStream out = getOutputStream(defaultBufferSize);
376-
writeData(out);
377-
378-
try (InputStream in = getInputStream(defaultBufferSize)) {
379-
final int len1 = dataLen / 4;
380-
// Read len1 bytes
381-
byte[] readData = new byte[len1];
382-
readAll(in, readData, 0, len1);
383-
byte[] expectedData = new byte[len1];
384-
System.arraycopy(data, 0, expectedData, 0, len1);
385-
Assert.assertArrayEquals(readData, expectedData);
386-
387-
// Pos: 1/3 dataLen
388-
byteBufferReadFullyCheck(in, dataLen / 3);
389-
390-
// Read len1 bytes
391-
readData = new byte[len1];
392-
readAll(in, readData, 0, len1);
393-
expectedData = new byte[len1];
394-
System.arraycopy(data, len1, expectedData, 0, len1);
395-
Assert.assertArrayEquals(readData, expectedData);
396-
397-
// Pos: 1/2 dataLen
398-
byteBufferReadFullyCheck(in, dataLen / 2);
399-
400-
// Read len1 bytes
401-
readData = new byte[len1];
402-
readAll(in, readData, 0, len1);
403-
expectedData = new byte[len1];
404-
System.arraycopy(data, 2 * len1, expectedData, 0, len1);
405-
Assert.assertArrayEquals(readData, expectedData);
406-
}
407-
}
408-
409-
private void byteBufferReadFullyCheck(InputStream in, int pos)
410-
throws Exception {
411-
ByteBuffer result = ByteBuffer.allocate(dataLen - pos);
412-
((ByteBufferPositionedReadable) in).readFully(pos, result);
413-
414-
byte[] expectedData = new byte[dataLen - pos];
415-
System.arraycopy(data, pos, expectedData, 0, dataLen - pos);
416-
Assert.assertArrayEquals(result.array(), expectedData);
417-
418-
result = ByteBuffer.allocate(dataLen); // Exceeds maximum length
419-
try {
420-
((ByteBufferPositionedReadable) in).readFully(pos, result);
421-
Assert.fail("Read fully exceeds maximum length should fail.");
422-
} catch (EOFException e) {
423-
}
424-
}
425372

426373
/** Test seek to different position. */
427374
@Test(timeout=120000)

0 commit comments

Comments
 (0)