Java Code Examples for org.apache.hadoop.fs.FSDataInputStream#readShort()

The following examples show how to use org.apache.hadoop.fs.FSDataInputStream#readShort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Display.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
protected InputStream getInputStream(PathData item) throws IOException {
  FSDataInputStream i = (FSDataInputStream)super.getInputStream(item);

  // Handle 0 and 1-byte files
  short leadBytes;
  try {
    leadBytes = i.readShort();
  } catch (EOFException e) {
    i.seek(0);
    return i;
  }

  // Check type of stream first
  switch(leadBytes) {
    case 0x1f8b: { // RFC 1952
      // Must be gzip
      i.seek(0);
      return new GZIPInputStream(i);
    }
    case 0x5345: { // 'S' 'E'
      // Might be a SequenceFile
      if (i.readByte() == 'Q') {
        i.close();
        return new TextRecordInputStream(item.stat);
      }
    }
    default: {
      // Check the type of compression instead, depending on Codec class's
      // own detection methods, based on the provided path.
      CompressionCodecFactory cf = new CompressionCodecFactory(getConf());
      CompressionCodec codec = cf.getCodec(item.path);
      if (codec != null) {
        i.seek(0);
        return codec.createInputStream(i);
      }
      break;
    }
    case 0x4f62: { // 'O' 'b'
      if (i.readByte() == 'j') {
        i.close();
        return new AvroFileInputStream(item.stat);
      }
      break;
    }
  }

  // File is non-compressed, or not a file container we know.
  i.seek(0);
  return i;
}
 
Example 2
Source File: Display.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
protected InputStream getInputStream(PathData item) throws IOException {
  FSDataInputStream i = (FSDataInputStream)super.getInputStream(item);

  // Handle 0 and 1-byte files
  short leadBytes;
  try {
    leadBytes = i.readShort();
  } catch (EOFException e) {
    i.seek(0);
    return i;
  }

  // Check type of stream first
  switch(leadBytes) {
    case 0x1f8b: { // RFC 1952
      // Must be gzip
      i.seek(0);
      return new GZIPInputStream(i);
    }
    case 0x5345: { // 'S' 'E'
      // Might be a SequenceFile
      if (i.readByte() == 'Q') {
        i.close();
        return new TextRecordInputStream(item.stat);
      }
    }
    default: {
      // Check the type of compression instead, depending on Codec class's
      // own detection methods, based on the provided path.
      CompressionCodecFactory cf = new CompressionCodecFactory(getConf());
      CompressionCodec codec = cf.getCodec(item.path);
      if (codec != null) {
        i.seek(0);
        return codec.createInputStream(i);
      }
      break;
    }
    case 0x4f62: { // 'O' 'b'
      if (i.readByte() == 'j') {
        i.close();
        return new AvroFileInputStream(item.stat);
      }
      break;
    }
  }

  // File is non-compressed, or not a file container we know.
  i.seek(0);
  return i;
}