java类java.io.DataInput的实例源码

DataSerializableJUnitTest.java 文件源码 项目:monarch 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Tests data serializing a <code>String</code> array using {@link DataSerializer#writeObject}.
 */
@Test
public void testStringArrayObject() throws Exception {
  Random random = getRandom();

  String[] array = new String[] {String.valueOf(random.nextLong()),
      String.valueOf(random.nextLong()), String.valueOf(random.nextLong())};

  DataOutputStream out = getDataOutput();
  DataSerializer.writeObject(array, out);
  out.flush();

  DataInput in = getDataInput();
  String[] array2 = (String[]) DataSerializer.readObject(in);

  assertEquals(array.length, array2.length);
  for (int i = 0; i < array.length; i++) {
    assertEquals(array[i], array2[i]);
  }
}
TransactionEventRecord.java 文件源码 项目:flume-release-1.7.0 阅读 34 收藏 0 点赞 0 评论 0
@Deprecated
static TransactionEventRecord fromDataInputV2(DataInput in)
    throws IOException {
  int header = in.readInt();
  if (header != MAGIC_HEADER) {
    throw new IOException("Header " + Integer.toHexString(header) +
        " is not the required value: " + Integer.toHexString(MAGIC_HEADER));
  }
  short type = in.readShort();
  long transactionID = in.readLong();
  long writeOrderID = in.readLong();
  TransactionEventRecord entry = newRecordForType(type, transactionID,
      writeOrderID);
  entry.readFields(in);
  return entry;
}
DiskInitFileParser.java 文件源码 项目:monarch 阅读 24 收藏 0 点赞 0 评论 0
private void readOplogMagicSeqRecord(DataInput dis, OPLOG_TYPE type) throws IOException {
  byte[] seq = new byte[OPLOG_TYPE.getLen()];
  dis.readFully(seq);
  for (int i = 0; i < OPLOG_TYPE.getLen(); i++) {
    if (seq[i] != type.getBytes()[i]) {
      if (logger.isTraceEnabled(LogMarker.PERSIST_RECOVERY)) {
        logger.trace(LogMarker.PERSIST_RECOVERY,
            "oplog magic code mismatched at byte:{}, value:{}", (i + 1), seq[i]);
      }
      throw new DiskAccessException("Invalid oplog (" + type.name() + ") file provided.",
          interpreter.getNameForError());
    }
  }
  if (logger.isTraceEnabled(LogMarker.PERSIST_RECOVERY)) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < OPLOG_TYPE.getLen(); i++) {
      sb.append(" " + seq[i]);
    }
    logger.trace(LogMarker.PERSIST_RECOVERY, "oplog magic code: {}", sb);
  }
  readEndOfRecord(dis);
}
DataSerializer.java 文件源码 项目:monarch 阅读 31 收藏 0 点赞 0 评论 0
/**
 * Reads a <code>IdentityHashMap</code> from a <code>DataInput</code>. Note that key identity is
 * not preserved unless the keys belong to a class whose serialization preserves identity.
 *
 * @throws IOException A problem occurs while reading from <code>in</code>
 * @throws ClassNotFoundException The class of one of the <Code>IdentityHashMap</code>'s elements
 *         cannot be found.
 *
 * @see #writeIdentityHashMap
 */
public static <K, V> IdentityHashMap<K, V> readIdentityHashMap(DataInput in)
    throws IOException, ClassNotFoundException {

  InternalDataSerializer.checkIn(in);

  int size = InternalDataSerializer.readArrayLength(in);
  if (size == -1) {
    return null;
  } else {
    IdentityHashMap<K, V> map = new IdentityHashMap<K, V>(size);
    for (int i = 0; i < size; i++) {
      K key = DataSerializer.<K>readObject(in);
      V value = DataSerializer.<V>readObject(in);
      map.put(key, value);
    }

    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
      logger.trace(LogMarker.SERIALIZER, "Read IdentityHashMap with {} elements: {}", size, map);
    }

    return map;
  }
}
Util.java 文件源码 项目:abhot 阅读 29 收藏 0 点赞 0 评论 0
public static long unpackUnsignedLong(DataInput buffer) throws IOException
{
    int shift = 0;
    long result = 0;
    while (shift < 64)
    {
        final byte b = buffer.readByte();
        result |= (long)(b & 0x7F) << shift;
        if ((b & 0x80) == 0)
        {
            return result;
        }
        shift += 7;
    }
    throw new IllegalArgumentException("Variable length quantity is too long");
}
HFileBlockIndex.java 文件源码 项目:ditb 阅读 35 收藏 0 点赞 0 评论 0
/**
 * Read in the root-level index from the given input stream. Must match
 * what was written into the root level by
 * {@link BlockIndexWriter#writeIndexBlocks(FSDataOutputStream)} at the
 * offset that function returned.
 *
 * @param in the buffered input stream or wrapped byte input stream
 * @param numEntries the number of root-level index entries
 * @throws IOException
 */
public void readRootIndex(DataInput in, final int numEntries)
    throws IOException {
  blockOffsets = new long[numEntries];
  blockKeys = new byte[numEntries][];
  blockDataSizes = new int[numEntries];

  // If index size is zero, no index was written.
  if (numEntries > 0) {
    for (int i = 0; i < numEntries; ++i) {
      long offset = in.readLong();
      int dataSize = in.readInt();
      byte[] key = Bytes.readByteArray(in);
      add(key, offset, dataSize);
    }
  }
}
SnapshotFSImageFormat.java 文件源码 项目:hadoop 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Load snapshots and snapshotQuota for a Snapshottable directory.
 *
 * @param snapshottableParent
 *          The snapshottable directory for loading.
 * @param numSnapshots
 *          The number of snapshots that the directory has.
 * @param loader
 *          The loader
 */
public static void loadSnapshotList(INodeDirectory snapshottableParent,
    int numSnapshots, DataInput in, FSImageFormat.Loader loader)
    throws IOException {
  DirectorySnapshottableFeature sf = snapshottableParent
      .getDirectorySnapshottableFeature();
  Preconditions.checkArgument(sf != null);
  for (int i = 0; i < numSnapshots; i++) {
    // read snapshots
    final Snapshot s = loader.getSnapshot(in);
    s.getRoot().setParent(snapshottableParent);
    sf.addSnapshot(s);
  }
  int snapshotQuota = in.readInt();
  snapshottableParent.setSnapshotQuota(snapshotQuota);
}
AbstractMapWritable.java 文件源码 项目:hadoop 阅读 37 收藏 0 点赞 0 评论 0
@Override
public void readFields(DataInput in) throws IOException {

  // Get the number of "unknown" classes

  newClasses = in.readByte();

  // Then read in the class names and add them to our tables

  for (int i = 0; i < newClasses; i++) {
    byte id = in.readByte();
    String className = in.readUTF();
    try {
      addToMap(Class.forName(className), id);

    } catch (ClassNotFoundException e) {
      throw new IOException("can't find class: " + className + " because "+
          e.getMessage());
    }
  }
}
PdxType.java 文件源码 项目:monarch 阅读 35 收藏 0 点赞 0 评论 0
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
  this.className = DataSerializer.readString(in);
  swizzleGemFireClassNames();
  {
    byte bits = in.readByte();
    this.noDomainClass = (bits & NO_DOMAIN_CLASS_BIT) != 0;
    this.hasDeletedField = (bits & HAS_DELETED_FIELD_BIT) != 0;
  }

  this.typeId = in.readInt();
  this.vlfCount = in.readInt();

  int arrayLen = InternalDataSerializer.readArrayLength(in);

  for (int i = 0; i < arrayLen; i++) {
    PdxField vft = new PdxField();
    vft.fromData(in);
    addField(vft);
  }
}
AXMLParser.java 文件源码 项目:axmlparser 阅读 40 收藏 0 点赞 0 评论 0
private final void doStart() throws IOException {
    ReadUtil.readCheckType(m_stream, AXML_CHUNK_TYPE);
    /*chunk size*/
    ReadUtil.readInt(m_stream);

    m_strings = StringBlock.read(new ExtDataInput((DataInput) new LittleEndianDataInputStream(m_stream)));

    ReadUtil.readCheckType(m_stream, RESOURCEIDS_CHUNK_TYPE);
    int chunkSize = ReadUtil.readInt(m_stream);
    if (chunkSize < 8 || (chunkSize % 4) != 0) {
        throw new IOException("Invalid resource ids size (" + chunkSize + ").");
    }
    m_resourceIDs = ReadUtil.readIntArray(m_stream, chunkSize / 4 - 2);

    resetState();
}
DataSerializableJUnitTest.java 文件源码 项目:monarch 阅读 44 收藏 0 点赞 0 评论 0
/**
 * Tests data serializing an {@link Stack}
 */
@Test
public void testStack() throws Exception {
  Random random = getRandom();
  Stack list = new Stack();
  int size = random.nextInt(50);
  for (int i = 0; i < size; i++) {
    list.add(new Long(random.nextLong()));
  }

  DataOutputStream out = getDataOutput();
  DataSerializer.writeStack(list, out);
  out.flush();

  DataInput in = getDataInput();
  Stack list2 = DataSerializer.readStack(in);
  assertEquals(list, list2);
}
DataSerializableJUnitTest.java 文件源码 项目:monarch 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Tests data serializing an {@link TreeMap} using {@link DataSerializer#writeObject}.
 */
@Test
public void testTreeMapObject() throws Exception {
  Random random = getRandom();
  TreeMap map = new TreeMap();
  int size = random.nextInt(50);
  for (int i = 0; i < size; i++) {
    Object key = new Long(random.nextLong());
    Object value = String.valueOf(random.nextLong());
    map.put(key, value);
  }

  DataOutputStream out = getDataOutput();
  DataSerializer.writeObject(map, out);
  out.flush();

  DataInput in = getDataInput();
  TreeMap map2 = (TreeMap) DataSerializer.readObject(in);
  assertEquals(map, map2);
}
FixedFileTrailer.java 文件源码 项目:ditb 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Deserialize the file trailer as writable data
 * @param input
 * @throws IOException
 */
void deserializeFromWritable(DataInput input) throws IOException {
  fileInfoOffset = input.readLong();
  loadOnOpenDataOffset = input.readLong();
  dataIndexCount = input.readInt();
  uncompressedDataIndexSize = input.readLong();
  metaIndexCount = input.readInt();

  totalUncompressedBytes = input.readLong();
  entryCount = input.readLong();
  compressionCodec = Compression.Algorithm.values()[input.readInt()];
  numDataIndexLevels = input.readInt();
  firstDataBlockOffset = input.readLong();
  lastDataBlockOffset = input.readLong();
  // TODO this is a classname encoded into an  HFile's trailer. We are going to need to have 
  // some compat code here.
  setComparatorClass(getComparatorClass(Bytes.readStringFixedSize(input,
      MAX_COMPARATOR_NAME_LENGTH)));
}
ObjectPartList.java 文件源码 项目:monarch 阅读 41 收藏 0 点赞 0 评论 0
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
  this.hasKeys = in.readBoolean();
  if (this.hasKeys) {
    this.keys = new ArrayList();
  }
  int numObjects = in.readInt();
  if (numObjects > 0) {
    for (int index = 0; index < numObjects; ++index) {
      if (this.hasKeys) {
        Object key = DataSerializer.readObject(in);
        this.keys.add(key);
      }
      boolean isException = in.readBoolean();
      Object value;
      if (isException) {
        byte[] exBytes = DataSerializer.readByteArray(in);
        value = CacheServerHelper.deserialize(exBytes);
        // ignore the exception string meant for native clients
        DataSerializer.readString(in);
      } else {
        value = DataSerializer.readObject(in);
      }
      this.objects.add(value);
    }
  }
}
MDRangeFilter.java 文件源码 项目:ditb 阅读 32 收藏 0 点赞 0 评论 0
@Override public void readFields(DataInput in) throws IOException {
  int size = in.readInt();
  ranges = new MDRange[size];
  for (int i = 0; i < size; i++) {
    int min = in.readInt();
    int max = in.readInt();
    ranges[i] = new MDRange(min, max);
  }
}
DLockQueryProcessor.java 文件源码 项目:monarch 阅读 30 收藏 0 点赞 0 评论 0
@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
  super.fromData(in);
  this.serviceName = DataSerializer.readString(in);
  this.objectName = DataSerializer.readObject(in);
  this.lockBatch = in.readBoolean();
  this.processorId = in.readInt();
}
Synthetic.java 文件源码 项目:openjdk-jdk10 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Construct object from input stream.
 *
 * @param name_index Index in constant pool to CONSTANT_Utf8
 * @param length Content length in bytes
 * @param input Input stream
 * @param constant_pool Array of constants
 * @throws IOException
 */
Synthetic(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
        throws IOException {
    this(name_index, length, (byte[]) null, constant_pool);
    if (length > 0) {
        bytes = new byte[length];
        input.readFully(bytes);
        System.err.println("Synthetic attribute with length > 0");
    }
}
UpdateEntryVersionOperation.java 文件源码 项目:monarch 阅读 36 收藏 0 点赞 0 评论 0
@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
  super.fromData(in);
  this.eventId = (EventID) DataSerializer.readObject(in);
  this.key = DataSerializer.readObject(in);
  Boolean hasTailKey = DataSerializer.readBoolean(in);
  if (hasTailKey.booleanValue()) {
    this.tailKey = DataSerializer.readLong(in);
  }
}
ConfigurationRequest.java 文件源码 项目:monarch 阅读 28 收藏 0 点赞 0 评论 0
@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
  this.isRequestForEntireConfiguration = in.readBoolean();
  int size = in.readInt();
  Set<String> groups = new HashSet<String>();
  if (size > 0) {
    for (int i = 0; i < size; i++) {
      groups.add(in.readUTF());
    }
  }
  this.groups = groups;
  this.numAttempts = in.readInt();
}
TFile.java 文件源码 项目:hadoop 阅读 34 收藏 0 点赞 0 评论 0
public TFileMeta(DataInput in) throws IOException {
  version = new Version(in);
  if (!version.compatibleWith(TFile.API_VERSION)) {
    throw new RuntimeException("Incompatible TFile fileVersion.");
  }
  recordCount = Utils.readVLong(in);
  strComparator = Utils.readString(in);
  comparator = makeComparator(strComparator);
}
GMSMember.java 文件源码 项目:monarch 阅读 27 收藏 0 点赞 0 评论 0
@Override
public void readAdditionalData(DataInput in) throws ClassNotFoundException, IOException {
  try {
    this.uuidMSBs = in.readLong();
    this.uuidLSBs = in.readLong();
    memberWeight = (byte) (in.readByte() & 0xFF);
  } catch (EOFException e) {
    // some IDs do not have UUID or membership weight information
  }
}
InternalDataSerializer.java 文件源码 项目:monarch 阅读 30 收藏 0 点赞 0 评论 0
/** read a set of Long objects */
public static List<Long> readListOfLongs(DataInput in) throws IOException {
  int size = in.readInt();
  if (size < 0) {
    return null;
  } else {
    List result = new LinkedList();
    boolean longIDs = in.readBoolean();
    for (int i = 0; i < size; i++) {
      long l = longIDs ? in.readLong() : in.readInt();
      result.add(Long.valueOf(l));
    }
    return result;
  }
}
DelegationTokenSecretManager.java 文件源码 项目:hadoop 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Load SecretManager state from fsimage.
 * 
 * @param in input stream to read fsimage
 * @throws IOException
 */
public synchronized void loadSecretManagerStateCompat(DataInput in)
    throws IOException {
  if (running) {
    // a safety check
    throw new IOException(
        "Can't load state from image in a running SecretManager.");
  }
  serializerCompat.load(in);
}
Bytes.java 文件源码 项目:aliyun-tablestore-hbase-client 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Read byte-array written with a WritableableUtils.vint prefix.
 * @param in Input to read from.
 * @return byte array read off <code>in</code>
 * @throws IOException e
 */
public static byte[] readByteArray(final DataInput in) throws IOException {
  int len = WritableUtils.readVInt(in);
  if (len < 0) {
    throw new NegativeArraySizeException(Integer.toString(len));
  }
  byte[] result = new byte[len];
  in.readFully(result, 0, len);
  return result;
}
GenericWritable.java 文件源码 项目:hadoop-oss 阅读 27 收藏 0 点赞 0 评论 0
@Override
public void readFields(DataInput in) throws IOException {
  type = in.readByte();
  Class<? extends Writable> clazz = getTypes()[type & 0xff];
  try {
    instance = ReflectionUtils.newInstance(clazz, conf);
  } catch (Exception e) {
    e.printStackTrace();
    throw new IOException("Cannot initialize the class: " + clazz);
  }
  instance.readFields(in);
}
PutMessage.java 文件源码 项目:monarch 阅读 32 收藏 0 点赞 0 评论 0
@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
  super.fromData(in);
  this.result = in.readBoolean();
  this.op = Operation.fromOrdinal(in.readByte());
  this.oldValue = DataSerializer.readObject(in);
  this.versionTag = (VersionTag) DataSerializer.readObject(in);
}
DefaultRecord.java 文件源码 项目:kafka-0.11.0.0-src-with-comment 阅读 26 收藏 0 点赞 0 评论 0
public static DefaultRecord readFrom(DataInput input,
                                     long baseOffset,
                                     long baseTimestamp,
                                     int baseSequence,
                                     Long logAppendTime) throws IOException {
    int sizeOfBodyInBytes = ByteUtils.readVarint(input);
    ByteBuffer recordBuffer = ByteBuffer.allocate(sizeOfBodyInBytes);
    input.readFully(recordBuffer.array(), 0, sizeOfBodyInBytes);
    int totalSizeInBytes = ByteUtils.sizeOfVarint(sizeOfBodyInBytes) + sizeOfBodyInBytes;
    return readFrom(recordBuffer, totalSizeInBytes, sizeOfBodyInBytes, baseOffset, baseTimestamp,
            baseSequence, logAppendTime);
}
SerializationHelper.java 文件源码 项目:monarch 阅读 36 收藏 0 点赞 0 评论 0
public static HashSet/* <ServerLocation> */ readServerLocationSet(DataInput in)
    throws IOException, ClassNotFoundException {
  int size = in.readInt();
  if (size < 0) {
    return null;
  }
  HashSet serverLocations = new HashSet(size);
  for (int i = 0; i < size; i++) {
    ServerLocation next = new ServerLocation();
    InternalDataSerializer.invokeFromData(next, in);
    serverLocations.add(next);
  }
  return serverLocations;
}
AndNode.java 文件源码 项目:aika 阅读 27 收藏 0 点赞 0 评论 0
@Override
public void readFields(DataInput in, Model m) throws IOException {
    super.readFields(in, m);

    int s = in.readInt();
    for(int i = 0; i < s; i++) {
        Refinement ref = Refinement.read(in, m);
        Provider<? extends Node> pn = m.lookupNodeProvider(in.readInt());
        parents.put(ref, pn);
    }
}
LittleEndianDataInputStreamTest.java 文件源码 项目:guava-mock 阅读 38 收藏 0 点赞 0 评论 0
public void testReadUnsignedByte_eof() throws IOException {
  DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(new byte[0]));
  try {
    in.readUnsignedByte();
    fail();
  } catch (EOFException expected) {
  }
}


问题


面经


文章

微信
公众号

扫码关注公众号