Java Code Examples for java.io.DataInputStream#readInt()

The following examples show how to use java.io.DataInputStream#readInt() . 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: Checkpoints.java    From flink with Apache License 2.0 6 votes vote down vote up
public static Savepoint loadCheckpointMetadata(DataInputStream in, ClassLoader classLoader) throws IOException {
	checkNotNull(in, "input stream");
	checkNotNull(classLoader, "classLoader");

	final int magicNumber = in.readInt();

	if (magicNumber == HEADER_MAGIC_NUMBER) {
		final int version = in.readInt();
		final SavepointSerializer<?> serializer = SavepointSerializers.getSerializer(version);

		if (serializer != null) {
			return serializer.deserialize(in, classLoader);
		}
		else {
			throw new IOException("Unrecognized checkpoint version number: " + version);
		}
	}
	else {
		throw new IOException("Unexpected magic number. This can have multiple reasons: " +
				"(1) You are trying to load a Flink 1.0 savepoint, which is not supported by this " +
				"version of Flink. (2) The file you were pointing to is not a savepoint at all. " +
				"(3) The savepoint file has been corrupted.");
	}
}
 
Example 2
Source File: CompiledCode.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
CompiledCode( byte[] code_block ) throws IOException {
    int idx;
    
    ByteArrayInputStream bis = new ByteArrayInputStream(code_block);
    DataInputStream source = new DataInputStream(bis);

    max_stack = source.readUnsignedShort();
    max_locals = source.readUnsignedShort();
    code_length = source.readInt();
    code = new byte[code_length];
    source.read(code);
    exception_table_length = source.readUnsignedShort();
    exceptionTable = new ExceptionTableEntry[exception_table_length];
    for (int i=0; i<exception_table_length; i++) {
      exceptionTable[i] = new ExceptionTableEntry(source);
    }
    attributes_count = source.readUnsignedShort();
    attributes_info = new CompiledAttribute[attributes_count];
    for (idx=0; idx<attributes_count; idx++) {
        attributes_info[idx] = new CompiledAttribute(source);
    }
}
 
Example 3
Source File: FunctionOperator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void readFunction()
{
  try {
    if (statelessF != null || statefulF.getValue() != null) {
      return;
    }
    DataInputStream input = new DataInputStream(new ByteArrayInputStream(annonymousFunctionClass));
    byte[] classNameBytes = new byte[input.readInt()];
    input.read(classNameBytes);
    String className = new String(classNameBytes);
    byte[] classData = new byte[input.readInt()];
    input.read(classData);
    Map<String, byte[]> classBin = new HashMap<>();
    classBin.put(className, classData);
    ByteArrayClassLoader byteArrayClassLoader = new ByteArrayClassLoader(classBin, Thread.currentThread().getContextClassLoader());
    statelessF = ((Class<FUNCTION>)byteArrayClassLoader.findClass(className)).newInstance();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: BinaryAttribute.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Load a list of attributes
 */
public static BinaryAttribute load(DataInputStream in, BinaryConstantPool cpool, int mask) throws IOException {
    BinaryAttribute atts = null;
    int natt = in.readUnsignedShort();  // JVM 4.6 method_info.attrutes_count

    for (int i = 0 ; i < natt ; i++) {
        // id from JVM 4.7 attribute_info.attribute_name_index
        Identifier id = cpool.getIdentifier(in.readUnsignedShort());
        // id from JVM 4.7 attribute_info.attribute_length
        int len = in.readInt();

        if (id.equals(idCode) && ((mask & ATT_CODE) == 0)) {
            in.skipBytes(len);
        } else {
            byte data[] = new byte[len];
            in.readFully(data);
            atts = new BinaryAttribute(id, data, atts);
        }
    }
    return atts;
}
 
Example 5
Source File: LocalScreen.java    From Alite with GNU General Public License v3.0 6 votes vote down vote up
public static boolean initialize(Alite alite, final DataInputStream dis) {
	LocalScreen ls = new LocalScreen(alite);
	try {
		ls.zoomFactor = dis.readFloat();
		ls.centerX = dis.readInt();
		ls.centerY = dis.readInt();
		ls.pendingZoomFactor = ls.zoomFactor;
		ls.pendingCenterX    = ls.centerX;
		ls.pendingCenterY    = ls.centerY;
	} catch (Exception e) {
		AliteLog.e("Local Screen Initialize", "Error in initializer.", e);
		return false;
	}
	alite.setScreen(ls);
	return true;
}
 
Example 6
Source File: FixedFileTrailer.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the fixed file trailer from the given stream. The version needs
 * to already be specified. Make sure this is consistent with
 * {@link #serialize(DataOutputStream)}.
 */
void deserialize(DataInputStream inputStream) throws IOException {
  HFile.checkFormatVersion(majorVersion);

  BlockType.TRAILER.readAndCheck(inputStream);

  if (majorVersion > 2
    || (majorVersion == 2 && minorVersion >= HFileReaderImpl.PBUF_TRAILER_MINOR_VERSION)) {
    deserializeFromPB(inputStream);
  } else {
    deserializeFromWritable(inputStream);
  }

  // The last 4 bytes of the file encode the major and minor version universally
  int version = inputStream.readInt();
  expectMajorVersion(extractMajorVersion(version));
  expectMinorVersion(extractMinorVersion(version));
}
 
Example 7
Source File: ThreadSafeBitSet.java    From zeno with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize a ThreadSafeBitSet from an InputStream
 */
public static ThreadSafeBitSet deserializeFrom(DataInputStream dis) throws IOException {
    int log2SegmentSize = dis.read();

    int numLongsPerSegment = (1 << (log2SegmentSize - 6));

    int numSegments = dis.readInt();

    ThreadSafeBitSetSegments segments = new ThreadSafeBitSetSegments(numSegments, numLongsPerSegment);

    for(int i=0;i<segments.numSegments();i++) {
        AtomicLongArray arr = segments.getSegment(i);

        for(int j=0;j<numLongsPerSegment;j++) {
            arr.set(j, dis.readLong());
        }
    }

    return new ThreadSafeBitSet(segments, log2SegmentSize);
}
 
Example 8
Source File: IonoGalileo.java    From GNSS_Compare with Apache License 2.0 5 votes vote down vote up
@Override
public void read(DataInputStream dai, boolean oldVersion) throws IOException {
	int v=1;
	if(!oldVersion) v=dai.readInt();

	if(v==1){

		health = dai.readLong();
		utcA1 = dai.readDouble();
		utcA0 = dai.readDouble();
		utcTOW = dai.readLong();
		utcWNT = dai.readInt();
		utcLS = dai.readInt();
		utcWNF = dai.readInt();
		utcDN = dai.readInt();
		utcLSF = dai.readInt();
		for(int i=0;i<alpha.length;i++){
			alpha[i] = dai.readFloat();
		}
		for(int i=0;i<beta.length;i++){
			beta[i] = dai.readFloat();
		}
		validHealth = dai.readBoolean();
		validUTC = dai.readBoolean();
		validKlobuchar = dai.readBoolean();
		long l = dai.readLong();
		refTime = new Time(l>0?l:System.currentTimeMillis());
	}else{
		throw new IOException("Unknown format version:"+v);
	}
}
 
Example 9
Source File: Utilities.java    From T0rlib4j with Apache License 2.0 5 votes vote down vote up
public static Socket socks4aSocketConnection(String networkHost, int networkPort, String socksHost, int socksPort)
        throws IOException {

    Socket socket = new Socket();
    socket.setSoTimeout(READ_TIMEOUT_MILLISECONDS);
    SocketAddress socksAddress = new InetSocketAddress(socksHost, socksPort);
    socket.connect(socksAddress, CONNECT_TIMEOUT_MILLISECONDS);

    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
    outputStream.write((byte) 0x04);
    outputStream.write((byte) 0x01);
    outputStream.writeShort((short) networkPort);
    outputStream.writeInt(0x01);
    outputStream.write((byte) 0x00);
    outputStream.write(networkHost.getBytes());
    outputStream.write((byte) 0x00);

    DataInputStream inputStream = new DataInputStream(socket.getInputStream());
    byte firstByte = inputStream.readByte();
    byte secondByte = inputStream.readByte();
    if (firstByte != (byte) 0x00 || secondByte != (byte) 0x5a) {
        socket.close();
        throw new IOException("SOCKS4a connect failed, got " + firstByte + " - " + secondByte +
                ", but expected 0x00 - 0x5a");
    }
    inputStream.readShort();
    inputStream.readInt();
    return socket;
}
 
Example 10
Source File: StringDeserializer.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<String> create(final InputStream arg) {
  final DataInputStream dis = new DataInputStream(arg);
  return new Iterable<String>() {

    @Override
    public Iterator<String> iterator() {
      return new Iterator<String>() {

        @Override
        public void remove() {
          throw new UnsupportedOperationException();
        }

        @Override
        public String next() {
          int len = 0;
          try {
            len = dis.readInt();
            final byte[] b = new byte[len];
            dis.readFully(b);
            return new String(b, StandardCharsets.UTF_8);
          } catch (final IOException e) {
            throw new ServiceRuntimeException(e);
          }
        }

        @Override
        public boolean hasNext() {
          throw new UnsupportedOperationException();
        }
      };
    }
  };
}
 
Example 11
Source File: NFCompressedGraphPointersDeserializer.java    From netflix-graph with Apache License 2.0 5 votes vote down vote up
NFCompressedGraphPointers deserializePointers(DataInputStream dis) throws IOException {
    int numTypes = dis.readInt();

    /// Backwards compatibility:  The representation of the pointers is encoded as
    /// In order to maintain backwards compatibility of produced artifacts,
    /// if more than 32 bits is required to represent the pointers, then flag
    /// the sign bit in the serialized number of node types.
    if((numTypes & Integer.MIN_VALUE) != 0) {
        numTypes &= Integer.MAX_VALUE;
        return deserializeLongPointers(dis, numTypes & Integer.MAX_VALUE);
    }

    return deserializeIntPointers(dis, numTypes);
}
 
Example 12
Source File: BinaryCompressor.java    From reladomo with Apache License 2.0 5 votes vote down vote up
private String readString(DataInputStream dataIn) throws IOException
{
    int len = dataIn.readInt();
    byte[] bytes = new byte[len];
    dataIn.readFully(bytes);
    return new String(bytes, Charset.forName("UTF-8"));
}
 
Example 13
Source File: EntryModifiedNotificationMessage.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void readWire(final DataInputStream in) throws IOException, ClassNotFoundException {

      super.readWire(in);

      subscriberIdentity = in.readInt();

      final int eventListSize = in.readInt();
      events = new ArrayList<BinaryEntryModifiedEvent>(eventListSize);
      for (int i = 0; i < eventListSize; i++) {

         final BinaryEntryModifiedEvent event = new BinaryEntryModifiedEvent();
         event.readWire(in);
         events.add(event);
      }
   }
 
Example 14
Source File: Util.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static byte[] readByteBuffer(DataInputStream in) throws IOException {
    int b=in.read();
    if(b == 1) {
        b=in.readInt();
        byte[] buf=new byte[b];
        if (buf.length != in.read(buf, 0, buf.length)) {
          throw new IOException("Failed to read " + buf.length + "bytes"); // GemStoneAddition
        }
        return buf;
    }
    return null;
}
 
Example 15
Source File: ValueMetaBaseSerializationTest.java    From hop with Apache License 2.0 5 votes vote down vote up
private static void checkRestoring( ValueMetaBase initial ) throws Exception {
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  initial.writeMeta( new DataOutputStream( os ) );

  DataInputStream dataInputStream = new DataInputStream( new ByteArrayInputStream( os.toByteArray() ) );
  // an awkward hack, since readMetaData() expects object's type to have been read
  int restoredType = dataInputStream.readInt();
  assertEquals( "type", initial.getType(), restoredType );

  ValueMetaBase restored = new ValueMetaBase( initial.getName(), restoredType );
  restored.readMetaData( dataInputStream );

  assertMetaDataAreEqual( initial, restored );
}
 
Example 16
Source File: MessageInfoAndMetadataListSerde.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Deserialize the given stream and return the MessageInfo and Metadata lists.
 * @param stream the stream to deserialize from.
 * @param map the clustermap to use.
 * @param versionToDeserializeIn the SerDe version to use to deserialize.
 * @return the deserialized {@link MessageInfoAndMetadataListSerde}.
 * @throws IOException if an I/O error occurs while reading from the stream.
 */
static MessageInfoAndMetadataListSerde deserializeMessageInfoAndMetadataList(DataInputStream stream, ClusterMap map,
    short versionToDeserializeIn) throws IOException {
  if (versionToDeserializeIn >= VERSION_5) {
    short versionFromStream = stream.readShort();
    if (versionFromStream != versionToDeserializeIn) {
      throw new IllegalArgumentException(
          "Argument provided [" + versionToDeserializeIn + "] and stream [" + versionFromStream
              + "] disagree on version");
    }
  } else {
    versionToDeserializeIn =
        versionToDeserializeIn == DETERMINE_VERSION ? stream.readShort() : versionToDeserializeIn;
  }
  int messageCount = stream.readInt();
  ArrayList<MessageInfo> messageInfoList = new ArrayList<>(messageCount);
  ArrayList<MessageMetadata> messageMetadataList = new ArrayList<>(messageCount);
  for (int i = 0; i < messageCount; i++) {
    BlobId id = new BlobId(stream, map);
    long size = stream.readLong();
    long ttl = stream.readLong();
    boolean isDeleted = stream.readByte() == UPDATED;
    boolean isTtlUpdated = false;
    boolean isUndeleted = false;
    short lifeVersion = 0;
    Long crc = null;
    short accountId = Account.UNKNOWN_ACCOUNT_ID;
    short containerId = Container.UNKNOWN_CONTAINER_ID;
    long operationTime = Utils.Infinite_Time;
    if (versionToDeserializeIn < VERSION_1 || versionToDeserializeIn > VERSION_MAX) {
      throw new IllegalArgumentException("Unknown version to deserialize MessageInfoList " + versionToDeserializeIn);
    }
    if (versionToDeserializeIn >= VERSION_5) {
      isTtlUpdated = stream.readByte() == UPDATED;
    }
    if (versionToDeserializeIn >= VERSION_6) {
      isUndeleted = stream.readByte() == UPDATED;
    }
    if (versionToDeserializeIn > VERSION_1) {
      crc = stream.readByte() == FIELD_PRESENT ? stream.readLong() : null;
    }
    if (versionToDeserializeIn > VERSION_2) {
      accountId = stream.readShort();
      containerId = stream.readShort();
      operationTime = stream.readLong();
    }
    if (versionToDeserializeIn >= VERSION_6) {
      lifeVersion = stream.readShort();
    }

    messageInfoList.add(
        new MessageInfo(id, size, isDeleted, isTtlUpdated, isUndeleted, ttl, crc, accountId, containerId,
            operationTime, lifeVersion));

    if (versionToDeserializeIn > VERSION_3) {
      MessageMetadata messageMetadata =
          stream.readByte() == FIELD_PRESENT ? MessageMetadata.deserializeMessageMetadata(stream) : null;
      messageMetadataList.add(messageMetadata);
    } else {
      messageMetadataList.add(null);
    }
  }
  return new MessageInfoAndMetadataListSerde(messageInfoList, messageMetadataList, versionToDeserializeIn);
}
 
Example 17
Source File: RealClassBuilder.java    From amidst with GNU General Public License v3.0 4 votes vote down vote up
private RealClassConstant<Integer> readInteger(DataInputStream stream, byte type) throws IOException {
	int value = stream.readInt();
	return new RealClassConstant<>(type, value);
}
 
Example 18
Source File: StandardFlowFileCodec.java    From nifi with Apache License 2.0 4 votes vote down vote up
private String readString(final DataInputStream in) throws IOException {
    final int numBytes = in.readInt();
    final byte[] bytes = new byte[numBytes];
    StreamUtils.fillBuffer(in, bytes, true);
    return new String(bytes, "UTF-8");
}
 
Example 19
Source File: FSEditLogOp.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
void readFields(DataInputStream in, int logVersion)
    throws IOException {
  if (!NameNodeLayoutVersion.supports(
      LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) {
    this.length = in.readInt();
  }
  if (NameNodeLayoutVersion.supports(
      LayoutVersion.Feature.ADD_INODE_ID, logVersion)) {
    this.inodeId = in.readLong();
  } else {
    // The inodeId should be updated when this editLogOp is applied
    this.inodeId = INodeId.GRANDFATHER_INODE_ID;
  }
  if ((-17 < logVersion && length != 4) ||
      (logVersion <= -17 && length != 5 && !NameNodeLayoutVersion.supports(
          LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion))) {
    throw new IOException("Incorrect data format."  +
                          " logVersion is " + logVersion +
                          " but writables.length is " +
                          length + ". ");
  }
  this.path = FSImageSerialization.readString(in);

  if (NameNodeLayoutVersion.supports(
      LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) {
    this.replication = FSImageSerialization.readShort(in);
    this.mtime = FSImageSerialization.readLong(in);
  } else {
    this.replication = readShort(in);
    this.mtime = readLong(in);
  }

  if (NameNodeLayoutVersion.supports(
      LayoutVersion.Feature.FILE_ACCESS_TIME, logVersion)) {
    if (NameNodeLayoutVersion.supports(
        LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) {
      this.atime = FSImageSerialization.readLong(in);
    } else {
      this.atime = readLong(in);
    }
  } else {
    this.atime = 0;
  }

  if (NameNodeLayoutVersion.supports(
      LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) {
    this.blockSize = FSImageSerialization.readLong(in);
  } else {
    this.blockSize = readLong(in);
  }

  this.blocks = readBlocks(in, logVersion);
  this.permissions = PermissionStatus.read(in);

  if (this.opCode == OP_ADD) {
    aclEntries = AclEditLogUtil.read(in, logVersion);
    this.xAttrs = readXAttrsFromEditLog(in, logVersion);
    this.clientName = FSImageSerialization.readString(in);
    this.clientMachine = FSImageSerialization.readString(in);
    if (NameNodeLayoutVersion.supports(
        NameNodeLayoutVersion.Feature.CREATE_OVERWRITE, logVersion)) {
      this.overwrite = FSImageSerialization.readBoolean(in);
    } else {
      this.overwrite = false;
    }
    if (NameNodeLayoutVersion.supports(
        NameNodeLayoutVersion.Feature.BLOCK_STORAGE_POLICY, logVersion)) {
      this.storagePolicyId = FSImageSerialization.readByte(in);
    } else {
      this.storagePolicyId = BlockStoragePolicySuite.ID_UNSPECIFIED;
    }
    // read clientId and callId
    readRpcIds(in, logVersion);
  } else {
    this.clientName = "";
    this.clientMachine = "";
  }
}
 
Example 20
Source File: ClassFile.java    From JWebAssembly with Apache License 2.0 4 votes vote down vote up
/**
 * Load a class file and create a model of the class.
 *
 * @param stream
 *            The InputStream of the class file. Will be closed if finish.
 * @throws IOException
 *             if this input stream reaches the end before reading the class file.
 */
public ClassFile( InputStream stream ) throws IOException {
    DataInputStream input = new DataInputStream( stream );
    int magic = input.readInt();
    if( magic != 0xCAFEBABE ) {
        throw new IOException( "Invalid class magic: " + Integer.toHexString( magic ) );
    }
    minorVersion = input.readUnsignedShort();
    majorVersion = input.readUnsignedShort();

    constantPool = new ConstantPool( input );
    accessFlags = input.readUnsignedShort();
    thisClass = (ConstantClass)constantPool.get( input.readUnsignedShort() );
    superClass = (ConstantClass)constantPool.get( input.readUnsignedShort() );
    interfaces = new ConstantClass[input.readUnsignedShort()];
    for( int i = 0; i < interfaces.length; i++ ) {
        interfaces[i] = (ConstantClass)constantPool.get( input.readUnsignedShort() );
    }
    fields = readFields( input );
    methods = readMethods( input );
    attributes = new Attributes( input, constantPool );

    stream.close();

    AttributeInfo info = attributes.get( "Signature" );
    if( info != null ) {
        int idx = info.getDataInputStream().readShort();
        String signature = (String)constantPool.get( idx );
        int count = 0;
        for( int i = 0; i < signature.length(); i++ ) {
            char ch = signature.charAt( i );
            switch( ch ) {
                case '<':
                    count++;
                    continue;
                case '>':
                    count--;
                    continue;
            }
            if( count == 0 ) {
                thisSignature = signature.substring( 0, i );
                superSignature = signature.substring( i );
                break;
            }
        }
    }
}