List of usage examples for java.io DataInput readChar
char readChar() throws IOException;
From source file:RandomFileTest.java
public static String readFixedString(int size, DataInput dataInput) throws IOException { StringBuffer strBuffer = new StringBuffer(size); int count = 0; boolean more = true; while (more && count < size) { char aChar = dataInput.readChar(); count++;/* w w w .j a v a2 s.co m*/ if (aChar == 0) more = false; else strBuffer.append(aChar); } dataInput.skipBytes(2 * (size - count)); return strBuffer.toString(); }
From source file:RandomFileTest.java
public static String readFixedString(int size, DataInput in) throws IOException { StringBuilder b = new StringBuilder(size); int i = 0;/*from ww w.ja v a 2 s . c o m*/ boolean more = true; while (more && i < size) { char ch = in.readChar(); i++; if (ch == 0) more = false; else b.append(ch); } in.skipBytes(2 * (size - i)); return b.toString(); }
From source file:org.apache.geode.internal.InternalDataSerializer.java
/** * Reads an instance of {@code String} from a {@code DataInput} given the header byte already * being read. The return value may be {@code null}. * * @throws IOException A problem occurs while reading from {@code in} * * @since GemFire 5.7//from www .j a v a 2s.co m */ public static String readString(DataInput in, byte header) throws IOException { if (header == DSCODE.STRING_BYTES) { int len = in.readUnsignedShort(); if (logger.isTraceEnabled(LogMarker.SERIALIZER)) { logger.trace(LogMarker.SERIALIZER, "Reading STRING_BYTES of len={}", len); } byte[] buf = new byte[len]; in.readFully(buf, 0, len); return new String(buf, 0); // intentionally using deprecated constructor } else if (header == DSCODE.STRING) { if (logger.isTraceEnabled(LogMarker.SERIALIZER)) { logger.trace(LogMarker.SERIALIZER, "Reading utf STRING"); } return in.readUTF(); } else if (header == DSCODE.NULL_STRING) { if (logger.isTraceEnabled(LogMarker.SERIALIZER)) { logger.trace(LogMarker.SERIALIZER, "Reading NULL_STRING"); } return null; } else if (header == DSCODE.HUGE_STRING_BYTES) { int len = in.readInt(); if (logger.isTraceEnabled(LogMarker.SERIALIZER)) { logger.trace(LogMarker.SERIALIZER, "Reading HUGE_STRING_BYTES of len={}", len); } byte[] buf = new byte[len]; in.readFully(buf, 0, len); return new String(buf, 0); // intentionally using deprecated constructor } else if (header == DSCODE.HUGE_STRING) { int len = in.readInt(); if (logger.isTraceEnabled(LogMarker.SERIALIZER)) { logger.trace(LogMarker.SERIALIZER, "Reading HUGE_STRING of len={}", len); } char[] buf = new char[len]; for (int i = 0; i < len; i++) { buf[i] = in.readChar(); } return new String(buf); } else { String s = "Unknown String header " + header; throw new IOException(s); } }
From source file:org.apache.hadoop.chukwa.analysis.salsa.fsm.FSMIntermedEntry.java
public void readFields(DataInput in) throws IOException { int currlen, numkeys; this.state_type = new StateType(in.readInt()); this.state_mapred = new MapRedState(in.readInt()); this.state_hdfs = new HDFSState(in.readInt()); this.fsm_type = new FSMType(in.readInt()); in.readChar(); currlen = in.readInt();/*from w w w .j a va 2s . c om*/ if (currlen > 0) this.state_name = in.readUTF(); else this.state_name = ""; currlen = in.readInt(); if (currlen > 0) this.unique_id = in.readUTF(); else this.unique_id = ""; currlen = in.readInt(); if (currlen > 0) this.timestamp = in.readUTF(); else this.timestamp = ""; currlen = in.readInt(); if (currlen > 0) this.time_start = in.readUTF(); else this.time_start = ""; currlen = in.readInt(); if (currlen > 0) this.time_end = in.readUTF(); else this.time_end = ""; currlen = in.readInt(); if (currlen > 0) this.host_exec = in.readUTF(); else this.host_exec = ""; currlen = in.readInt(); if (currlen > 0) this.host_other = in.readUTF(); else this.host_other = ""; currlen = in.readInt(); if (currlen > 0) this.time_orig_epoch = in.readUTF(); else this.time_orig_epoch = ""; currlen = in.readInt(); if (currlen > 0) this.time_orig = in.readUTF(); else this.time_orig = ""; currlen = in.readInt(); if (currlen > 0) this.job_id = in.readUTF(); else this.job_id = ""; currlen = in.readInt(); if (currlen > 0) this.identifier = in.readUTF(); else this.identifier = ""; numkeys = in.readInt(); this.add_info = new TreeMap<String, String>(); if (numkeys > 0) { for (int i = 0; i < numkeys; i++) { String currkey, currval; currkey = in.readUTF(); currlen = in.readInt(); if (currlen > 0) { currval = in.readUTF(); this.add_info.put(currkey, currval); } } } }
From source file:org.apache.hadoop.hbase.io.HbaseObjectWritable.java
/** * Read a {@link Writable}, {@link String}, primitive type, or an array of * the preceding./*www . j av a 2 s.c o m*/ * @param in * @param objectWritable * @param conf * @return the object * @throws IOException */ @SuppressWarnings("unchecked") public static Object readObject(DataInput in, HbaseObjectWritable objectWritable, Configuration conf) throws IOException { Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in)); Object instance; if (declaredClass.isPrimitive()) { // primitive types if (declaredClass == Boolean.TYPE) { // boolean instance = Boolean.valueOf(in.readBoolean()); } else if (declaredClass == Character.TYPE) { // char instance = Character.valueOf(in.readChar()); } else if (declaredClass == Byte.TYPE) { // byte instance = Byte.valueOf(in.readByte()); } else if (declaredClass == Short.TYPE) { // short instance = Short.valueOf(in.readShort()); } else if (declaredClass == Integer.TYPE) { // int instance = Integer.valueOf(in.readInt()); } else if (declaredClass == Long.TYPE) { // long instance = Long.valueOf(in.readLong()); } else if (declaredClass == Float.TYPE) { // float instance = Float.valueOf(in.readFloat()); } else if (declaredClass == Double.TYPE) { // double instance = Double.valueOf(in.readDouble()); } else if (declaredClass == Void.TYPE) { // void instance = null; } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isArray()) { // array if (declaredClass.equals(byte[].class)) { instance = Bytes.readByteArray(in); } else if (declaredClass.equals(Result[].class)) { instance = Result.readArray(in); } else { int length = in.readInt(); instance = Array.newInstance(declaredClass.getComponentType(), length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } } else if (declaredClass.equals(Array.class)) { //an array not declared in CLASS_TO_CODE Class<?> componentType = readClass(conf, in); int length = in.readInt(); instance = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } else if (List.class.isAssignableFrom(declaredClass)) { // List int length = in.readInt(); instance = new ArrayList(length); for (int i = 0; i < length; i++) { ((ArrayList) instance).add(readObject(in, conf)); } } else if (declaredClass == String.class) { // String instance = Text.readString(in); } else if (declaredClass.isEnum()) { // enum instance = Enum.valueOf((Class<? extends Enum>) declaredClass, Text.readString(in)); } else if (declaredClass == Message.class) { String className = Text.readString(in); try { declaredClass = getClassByName(conf, className); instance = tryInstantiateProtobuf(declaredClass, in); } catch (ClassNotFoundException e) { LOG.error("Can't find class " + className, e); throw new IOException("Can't find class " + className, e); } } else { // Writable or Serializable Class instanceClass = null; int b = (byte) WritableUtils.readVInt(in); if (b == NOT_ENCODED) { String className = Text.readString(in); try { instanceClass = getClassByName(conf, className); } catch (ClassNotFoundException e) { LOG.error("Can't find class " + className, e); throw new IOException("Can't find class " + className, e); } } else { instanceClass = CODE_TO_CLASS.get(b); } if (Writable.class.isAssignableFrom(instanceClass)) { Writable writable = WritableFactories.newInstance(instanceClass, conf); try { writable.readFields(in); } catch (Exception e) { LOG.error("Error in readFields", e); throw new IOException("Error in readFields", e); } instance = writable; if (instanceClass == NullInstance.class) { // null declaredClass = ((NullInstance) instance).declaredClass; instance = null; } } else { int length = in.readInt(); byte[] objectBytes = new byte[length]; in.readFully(objectBytes); ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { bis = new ByteArrayInputStream(objectBytes); ois = new ObjectInputStream(bis); instance = ois.readObject(); } catch (ClassNotFoundException e) { LOG.error("Class not found when attempting to deserialize object", e); throw new IOException("Class not found when attempting to " + "deserialize object", e); } finally { if (bis != null) bis.close(); if (ois != null) ois.close(); } } } if (objectWritable != null) { // store values objectWritable.declaredClass = declaredClass; objectWritable.instance = instance; } return instance; }
From source file:org.apache.hadoop.hbase.security.access.HbaseObjectWritableFor96Migration.java
/** * Read a {@link Writable}, {@link String}, primitive type, or an array of * the preceding./*ww w. ja va2 s.c o m*/ * @param in * @param objectWritable * @param conf * @return the object * @throws IOException */ @SuppressWarnings("unchecked") static Object readObject(DataInput in, HbaseObjectWritableFor96Migration objectWritable, Configuration conf) throws IOException { Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in)); Object instance; if (declaredClass.isPrimitive()) { // primitive types if (declaredClass == Boolean.TYPE) { // boolean instance = Boolean.valueOf(in.readBoolean()); } else if (declaredClass == Character.TYPE) { // char instance = Character.valueOf(in.readChar()); } else if (declaredClass == Byte.TYPE) { // byte instance = Byte.valueOf(in.readByte()); } else if (declaredClass == Short.TYPE) { // short instance = Short.valueOf(in.readShort()); } else if (declaredClass == Integer.TYPE) { // int instance = Integer.valueOf(in.readInt()); } else if (declaredClass == Long.TYPE) { // long instance = Long.valueOf(in.readLong()); } else if (declaredClass == Float.TYPE) { // float instance = Float.valueOf(in.readFloat()); } else if (declaredClass == Double.TYPE) { // double instance = Double.valueOf(in.readDouble()); } else if (declaredClass == Void.TYPE) { // void instance = null; } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isArray()) { // array if (declaredClass.equals(byte[].class)) { instance = Bytes.readByteArray(in); } else { int length = in.readInt(); instance = Array.newInstance(declaredClass.getComponentType(), length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } } else if (declaredClass.equals(Array.class)) { //an array not declared in CLASS_TO_CODE Class<?> componentType = readClass(conf, in); int length = in.readInt(); instance = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } else if (List.class.isAssignableFrom(declaredClass)) { // List int length = in.readInt(); instance = new ArrayList(length); for (int i = 0; i < length; i++) { ((ArrayList) instance).add(readObject(in, conf)); } } else if (declaredClass == String.class) { // String instance = Text.readString(in); } else if (declaredClass.isEnum()) { // enum instance = Enum.valueOf((Class<? extends Enum>) declaredClass, Text.readString(in)); } else if (declaredClass == Message.class) { String className = Text.readString(in); try { declaredClass = getClassByName(conf, className); instance = tryInstantiateProtobuf(declaredClass, in); } catch (ClassNotFoundException e) { LOG.error("Can't find class " + className, e); throw new IOException("Can't find class " + className, e); } } else if (Scan.class.isAssignableFrom(declaredClass)) { int length = in.readInt(); byte[] scanBytes = new byte[length]; in.readFully(scanBytes); ClientProtos.Scan.Builder scanProto = ClientProtos.Scan.newBuilder(); instance = ProtobufUtil.toScan(scanProto.mergeFrom(scanBytes).build()); } else { // Writable or Serializable Class instanceClass = null; int b = (byte) WritableUtils.readVInt(in); if (b == NOT_ENCODED) { String className = Text.readString(in); try { instanceClass = getClassByName(conf, className); } catch (ClassNotFoundException e) { LOG.error("Can't find class " + className, e); throw new IOException("Can't find class " + className, e); } } else { instanceClass = CODE_TO_CLASS.get(b); } if (Writable.class.isAssignableFrom(instanceClass)) { Writable writable = WritableFactories.newInstance(instanceClass, conf); try { writable.readFields(in); } catch (Exception e) { LOG.error("Error in readFields", e); throw new IOException("Error in readFields", e); } instance = writable; if (instanceClass == NullInstance.class) { // null declaredClass = ((NullInstance) instance).declaredClass; instance = null; } } else { int length = in.readInt(); byte[] objectBytes = new byte[length]; in.readFully(objectBytes); ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { bis = new ByteArrayInputStream(objectBytes); ois = new ObjectInputStream(bis); instance = ois.readObject(); } catch (ClassNotFoundException e) { LOG.error("Class not found when attempting to deserialize object", e); throw new IOException("Class not found when attempting to " + "deserialize object", e); } finally { if (bis != null) bis.close(); if (ois != null) ois.close(); } } } if (objectWritable != null) { // store values objectWritable.declaredClass = declaredClass; objectWritable.instance = instance; } return instance; }
From source file:org.cloudata.core.common.io.CObjectWritable.java
/** Read a {@link CWritable}, {@link String}, primitive type, or an array of * the preceding. *//*from w ww. j a va 2 s . c o m*/ @SuppressWarnings("unchecked") public static Object readObject(DataInput in, CObjectWritable objectWritable, CloudataConf conf, boolean arrayComponent, Class componentClass) throws IOException { String className; if (arrayComponent) { className = componentClass.getName(); } else { className = CUTF8.readString(in); //SANGCHUL // System.out.println("SANGCHUL] className:" + className); } Class<?> declaredClass = PRIMITIVE_NAMES.get(className); if (declaredClass == null) { try { declaredClass = conf.getClassByName(className); } catch (ClassNotFoundException e) { //SANGCHUL e.printStackTrace(); throw new RuntimeException("readObject can't find class[className=" + className + "]", e); } } Object instance; if (declaredClass.isPrimitive()) { // primitive types if (declaredClass == Boolean.TYPE) { // boolean instance = Boolean.valueOf(in.readBoolean()); } else if (declaredClass == Character.TYPE) { // char instance = Character.valueOf(in.readChar()); } else if (declaredClass == Byte.TYPE) { // byte instance = Byte.valueOf(in.readByte()); } else if (declaredClass == Short.TYPE) { // short instance = Short.valueOf(in.readShort()); } else if (declaredClass == Integer.TYPE) { // int instance = Integer.valueOf(in.readInt()); } else if (declaredClass == Long.TYPE) { // long instance = Long.valueOf(in.readLong()); } else if (declaredClass == Float.TYPE) { // float instance = Float.valueOf(in.readFloat()); } else if (declaredClass == Double.TYPE) { // double instance = Double.valueOf(in.readDouble()); } else if (declaredClass == Void.TYPE) { // void instance = null; } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isArray()) { // array //System.out.println("SANGCHUL] is array"); int length = in.readInt(); //System.out.println("SANGCHUL] array length : " + length); //System.out.println("Read:in.readInt():" + length); if (declaredClass.getComponentType() == Byte.TYPE) { byte[] bytes = new byte[length]; in.readFully(bytes); instance = bytes; } else if (declaredClass.getComponentType() == ColumnValue.class) { instance = readColumnValue(in, conf, declaredClass, length); } else { Class componentType = declaredClass.getComponentType(); // SANGCHUL //System.out.println("SANGCHUL] componentType : " + componentType.getName()); instance = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Object arrayComponentInstance = readObject(in, null, conf, !componentType.isArray(), componentType); Array.set(instance, i, arrayComponentInstance); //Array.set(instance, i, readObject(in, conf)); } } } else if (declaredClass == String.class) { // String instance = CUTF8.readString(in); } else if (declaredClass.isEnum()) { // enum instance = Enum.valueOf((Class<? extends Enum>) declaredClass, CUTF8.readString(in)); } else if (declaredClass == ColumnValue.class) { //ColumnValue? ?? ?? ? ? ?. //? ? ? ? ? ? . Class instanceClass = null; try { short typeDiff = in.readShort(); if (typeDiff == TYPE_DIFF) { instanceClass = conf.getClassByName(CUTF8.readString(in)); } else { instanceClass = declaredClass; } } catch (ClassNotFoundException e) { throw new RuntimeException("readObject can't find class", e); } ColumnValue columnValue = new ColumnValue(); columnValue.readFields(in); instance = columnValue; } else { // Writable Class instanceClass = null; try { short typeDiff = in.readShort(); // SANGCHUL //System.out.println("SANGCHUL] typeDiff : " + typeDiff); //System.out.println("Read:in.readShort():" + typeDiff); if (typeDiff == TYPE_DIFF) { // SANGCHUL String classNameTemp = CUTF8.readString(in); //System.out.println("SANGCHUL] typeDiff : " + classNameTemp); instanceClass = conf.getClassByName(classNameTemp); //System.out.println("Read:UTF8.readString(in):" + instanceClass.getClass()); } else { instanceClass = declaredClass; } } catch (ClassNotFoundException e) { // SANGCHUL e.printStackTrace(); throw new RuntimeException("readObject can't find class", e); } CWritable writable = CWritableFactories.newInstance(instanceClass, conf); writable.readFields(in); //System.out.println("Read:writable.readFields(in)"); instance = writable; if (instanceClass == NullInstance.class) { // null declaredClass = ((NullInstance) instance).declaredClass; instance = null; } } if (objectWritable != null) { // store values objectWritable.declaredClass = declaredClass; objectWritable.instance = instance; } return instance; }