/** * 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]); } }
@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; }
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); }
/** * 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; } }
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"); }
/** * 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); } } }
/** * 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); }
@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()); } } }
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); } }
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(); }
/** * 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); }
/** * 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); }
/** * 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))); }
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); } } }
@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); } }
@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(); }
/** * 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"); } }
@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); } }
@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(); }
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); }
@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 } }
/** 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; } }
/** * 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); }
/** * 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; }
@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); }
@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); }
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); }
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; }
@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); } }
public void testReadUnsignedByte_eof() throws IOException { DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(new byte[0])); try { in.readUnsignedByte(); fail(); } catch (EOFException expected) { } }
public static Timestamp readTimestamp(DataInput in) throws IOException { InternalDataSerializer.checkIn(in); Timestamp result = new Timestamp(DataSerializer.readPrimitiveLong(in)); if (logger.isTraceEnabled(LogMarker.SERIALIZER)) { logger.trace(LogMarker.SERIALIZER, "Read Timestamp: {}", result); } return result; }
@Override public void readFields(DataInput in) throws IOException { cumulativeCpuUsage = WritableUtils.readVLong(in); // long #1 cumulativeGpuUsage = WritableUtils.readVLong(in); virtualMemoryUsage = WritableUtils.readVLong(in); // long #2 physicalMemoryUsage = WritableUtils.readVLong(in); // long #3 heapUsage = WritableUtils.readVLong(in); // long #4 }
/** * Reads an object that was serialized by a customer ("user") <code>DataSerializer</code> from the * given <code>DataInput</code>. * * @throws IOException If the serializer that can deserialize the object is not registered. */ private static Object readUserObject(DataInput in, int serializerId) throws IOException, ClassNotFoundException { DataSerializer serializer = InternalDataSerializer.getSerializer(serializerId); if (serializer == null) { throw new IOException(LocalizedStrings.DataSerializer_SERIALIZER_0_IS_NOT_REGISTERED .toLocalizedString(new Object[] {Integer.valueOf(serializerId)})); } return serializer.fromData(in); }
public static MachoCommand read(final DataInput dataInput) throws IOException { final int commandId = dataInput.readInt(); final int commandSize = dataInput.readInt(); MachoCommand command; switch (commandId) { case LC_SEGMENT: case LC_SEGMENT_64: //System.out.println("LC_SEGMENT command " + commandId); command = new SegmentCommand(commandId); break; case LC_CODE_SIGNATURE: //System.out.println("LC_CODE_SIGNATURE command " + commandId); command = new CodeSignatureCommand(); break; default: //System.out.println("Unknown command " + commandId); command = new UnknownCommand(commandId, commandSize - 8); break; } command.readPayload(dataInput); if (command.getSize() != commandSize) { throw new IOException("Can't decode command in mach-o header. command.getSize()=" + command.getSize() + " commandSize=" + commandSize); } return command; }
@Override public void fromData(DataInput in) throws IOException, ClassNotFoundException { super.fromData(in); boolean hasPrimary = in.readBoolean(); if (hasPrimary) { primary = new InternalDistributedMember(); InternalDataSerializer.invokeFromData(primary, in); } }
@Override public void readFields(DataInput in) throws IOException { super.readFields(in); if (isMapOrReduce()) { splitMetaInfo.readFields(in); } }
@Override public void readFields(DataInput in, Model m) throws IOException { if (in.readBoolean()) { rid = in.readInt(); } neuron = m.lookupNeuron(in.readInt()); }
@Override public void readFields(DataInput in) throws IOException { int len = in.readInt(); byte[] inBytes = new byte[len]; in.readFully(inBytes); AuthenticationProtos.TokenIdentifier.Builder builder = AuthenticationProtos.TokenIdentifier.newBuilder(); ProtobufUtil.mergeFrom(builder, inBytes); AuthenticationProtos.TokenIdentifier identifier = builder.build(); // sanity check on type if (!identifier.hasKind() || identifier.getKind() != AuthenticationProtos.TokenIdentifier.Kind.HBASE_AUTH_TOKEN) { throw new IOException("Invalid TokenIdentifier kind from input "+identifier.getKind()); } // copy the field values if (identifier.hasUsername()) { username = identifier.getUsername().toStringUtf8(); } if (identifier.hasKeyId()) { keyId = identifier.getKeyId(); } if (identifier.hasIssueDate()) { issueDate = identifier.getIssueDate(); } if (identifier.hasExpirationDate()) { expirationDate = identifier.getExpirationDate(); } if (identifier.hasSequenceNumber()) { sequenceNumber = identifier.getSequenceNumber(); } }
@Override public void fromData(DataInput in) throws IOException, ClassNotFoundException { readEssentialData(in); this.directPort = in.readInt(); this.memberWeight = in.readByte(); this.vmKind = in.readByte(); this.processId = in.readInt(); this.name = DataSerializer.readString(in); this.groups = DataSerializer.readStringArray(in); }
public void fromDataPre_GFE_8_0_0_0(DataInput in) throws IOException, ClassNotFoundException { if (OLD_MEMBERS_OPTIMIZED) { this.myDataVersion = InternalDataSerializer.getVersionForDataStreamOrNull(in); this.myData = DataSerializer.readByteArray(in); } else { this.cqs = DataSerializer.readHashMap(in); this.interestedClients = InternalDataSerializer.readSetOfLongs(in); this.interestedClientsInv = InternalDataSerializer.readSetOfLongs(in); } }