public void read(DataInput in) throws InvalidByteCodeException, IOException { info = new byte[attributeLength]; in.readFully(info); if (debug) debug("read " + getDebugMessage()); }
/* * Read a String as a Network Int n, followed by n Bytes * Alternative to 16 bit read/writeUTF. * Encoding standard is... ? * */ public static String readString(DataInput in) throws IOException { int length = in.readInt(); if (length == -1) return null; byte[] buffer = new byte[length]; in.readFully(buffer); // could/should use readFully(buffer,0,length)? return new String(buffer, "UTF-8"); }
@Override public String deserialize(DataInput in, int available) throws IOException { int nsize = in.readInt(); byte[] buf = new byte[nsize]; in.readFully(buf); return bytArrayToHex(buf); }
/** * {@inheritDoc} * * @see Writable#readFields(DataInput) */ public void readFields(DataInput in) throws IOException { BSONDecoder dec = new BSONDecoder(); BSONCallback cb = new BasicBSONCallback(); // Read the BSON length from the start of the record int dataLen = in.readInt(); byte[] buf = new byte[dataLen]; in.readFully(buf); dec.decode(buf, cb); _doc = (BSONObject) cb.get(); log.info("Decoded a BSON Object: " + _doc); }
@Override public GeobufFeature deserialize(DataInput dataInput, int i) throws IOException { int len = dataInput.readInt(); byte[] feat = new byte[len]; dataInput.readFully(feat); Geobuf.Data data = Geobuf.Data.parseFrom(feat); return new GeobufFeature( data.getFeatureCollection().getFeatures(0), data.getKeysList(), Math.pow(10, data.getPrecision())); }
public static String getStringFromData(DataInput in, int size) throws IOException { try { // System.out.println("reading string of length " + size + "..."); byte buf[] = new byte[size]; in.readFully(buf); String result = new String(buf); // System.out.println("\"" + result + "\""); return (result); } catch (OutOfMemoryError e) { throw new IOException("String too long to read into memory: " + size); } catch (NegativeArraySizeException e) { throw new IOException("String length cannot be negative: " + size); } }
/** * Read a string, but check it for sanity. The format consists of a vint followed by the given * number of bytes. * * @param in the stream to read from * @param maxLength the largest acceptable length of the encoded string * @return the bytes as a string * @throws IOException if reading from the DataInput fails * @throws IllegalArgumentException if the encoded byte size for string is negative or larger than * maxSize. Only the vint is read. */ public static String readStringSafely(DataInput in, int maxLength) throws IOException, IllegalArgumentException { int length = readVInt(in); if (length < 0 || length > maxLength) { throw new IllegalArgumentException( "Encoded byte size for String was " + length + ", which is outside of 0.." + maxLength + " range."); } byte[] bytes = new byte[length]; in.readFully(bytes, 0, length); return Text.decode(bytes); }
public RangeSliceCommand deserialize(DataInput dis, int version) throws IOException { String keyspace = dis.readUTF(); String columnFamily = dis.readUTF(); int scLength = dis.readInt(); ByteBuffer superColumn = null; if (scLength > 0) { byte[] buf = new byte[scLength]; dis.readFully(buf); superColumn = ByteBuffer.wrap(buf); } TDeserializer dser = new TDeserializer(new TBinaryProtocol.Factory()); SlicePredicate pred = new SlicePredicate(); FBUtilities.deserialize(dser, pred, dis); List<IndexExpression> rowFilter = null; if (version >= MessagingService.VERSION_11) { int filterCount = dis.readInt(); rowFilter = new ArrayList<IndexExpression>(filterCount); for (int i = 0; i < filterCount; i++) { IndexExpression expr = new IndexExpression(); FBUtilities.deserialize(dser, expr, dis); rowFilter.add(expr); } } AbstractBounds<RowPosition> range = AbstractBounds.serializer().deserialize(dis, version).toRowBounds(); int maxResults = dis.readInt(); boolean maxIsColumns = false; boolean isPaging = false; if (version >= MessagingService.VERSION_11) { maxIsColumns = dis.readBoolean(); isPaging = dis.readBoolean(); } return new RangeSliceCommand( keyspace, columnFamily, superColumn, pred, range, rowFilter, maxResults, maxIsColumns, isPaging); }
public static byte[] readCompressedByteArray(DataInput in) throws IOException { int length = in.readInt(); if (length == -1) return null; byte[] buffer = new byte[length]; in.readFully(buffer); // could/should use readFully(buffer,0,length)? GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length)); byte[] outbuf = new byte[length]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) { bos.write(outbuf, 0, len); } byte[] decompressed = bos.toByteArray(); bos.close(); gzi.close(); return decompressed; }
public void readObject(DataInput in) throws IOException { boolean sgIO = in.readBoolean(); int nodeID = in.readInt(); int nodeClassID = in.readShort(); nodeClassName = null; if (nodeClassID == -1) nodeClassName = in.readUTF(); readConstructorParams(in); if (nodeClassID != -1) { node = createNode(); nodeClassName = node.getClass().getName(); } else node = createNode(nodeClassName); if (sgIO) { if (control.getCurrentFileVersion() == 1) ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO) node).readSceneGraphObject(in); else { int size = in.readInt(); if (node instanceof com.sun.j3d.utils.scenegraph.io.SceneGraphIO) { byte[] bytes = new byte[size]; in.readFully(bytes); ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); DataInputStream tmpIn = new DataInputStream(byteStream); ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO) node).readSceneGraphObject(tmpIn); tmpIn.close(); } else { in.skipBytes(size); } } } symbol = control.getSymbolTable().createSymbol(this, node, nodeID); readUserData(in); if (control.getCurrentFileVersion() > 2) { node.setName(readString(in)); } readCapabilities(in); }
public ReadResponse deserialize(DataInput dis, int version) throws IOException { byte[] digest = null; int digestSize = dis.readInt(); if (digestSize > 0) { digest = new byte[digestSize]; dis.readFully(digest, 0, digestSize); } boolean isDigest = dis.readBoolean(); assert isDigest == digestSize > 0; Row row = null; if (!isDigest) { // This is coming from a remote host row = Row.serializer.deserialize( dis, version, ColumnSerializer.Flag.FROM_REMOTE, ArrayBackedSortedColumns.factory()); } return isDigest ? new ReadResponse(ByteBuffer.wrap(digest)) : new ReadResponse(row); }
public void readFrom(DataInput in) throws Exception { // 1. read the leading byte first byte leading = in.readByte(); // 2. the flags flags = in.readShort(); // 3. dest_addr if (Util.isFlagSet(leading, DEST_SET)) dest_addr = Util.readAddress(in); // 4. src_addr if (Util.isFlagSet(leading, SRC_SET)) src_addr = Util.readAddress(in); // 5. buf if (Util.isFlagSet(leading, BUF_SET)) { int len = in.readInt(); buf = new byte[len]; in.readFully(buf, 0, len); length = len; } // 6. headers int len = in.readShort(); headers = createHeaders(len); short[] ids = headers.getRawIDs(); Header[] hdrs = headers.getRawHeaders(); for (int i = 0; i < len; i++) { short id = in.readShort(); Header hdr = readHeader(in); ids[i] = id; hdrs[i] = hdr; } }
public void readFrom(DataInput in) throws Exception { type = Type.values()[in.readByte()]; // We can't use Util.readObject since it's size is limited to 2^15-1 try { short first = in.readShort(); if (first == -1) { object = Util.readGenericStreamable(in); } else { ByteBuffer bb = ByteBuffer.allocate(4); bb.putShort(first); bb.putShort(in.readShort()); int size = bb.getInt(0); byte[] bytes = new byte[size]; in.readFully(bytes, 0, size); object = Util.objectFromByteBuffer(bytes); } } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException("Exception encountered while serializing execution request", e); } request = in.readLong(); }
public List<L2Property> readProperties( DataInput dataInput, String objClass, UnrealPackageReadOnly up) throws UnrealException { List<L2Property> properties = new ArrayList<>(); List<Property> classTemplate = unrealClassLoader.getStructProperties(objClass); Collections.reverse(classTemplate); try { String name; while (!(name = up.getNameTable().get(dataInput.readCompactInt()).getName()).equals("None")) { int info = dataInput.readUnsignedByte(); Type propertyType = Type.values()[info & 0b1111]; // TODO int sizeType = (info >> 4) & 0b111; boolean array = info >> 7 == 1; String structName = propertyType.equals(Type.STRUCT) ? up.getNameTable().get(dataInput.readCompactInt()).getName() : null; int size = readPropertySize(sizeType, dataInput); int arrayIndex = array && !propertyType.equals(Type.BOOL) ? dataInput.readCompactInt() : 0; byte[] objBytes = new byte[size]; dataInput.readFully(objBytes); final String n = name; PropertiesUtil.getAt(properties, n); L2Property property = PropertiesUtil.getAt(properties, n); if (property == null) { Property template = classTemplate .stream() .filter(pt -> pt.getEntry().getObjectName().getName().equalsIgnoreCase((n))) .findAny() .orElse(null); if (template == null) throw new UnrealException(objClass + ": Property template not found: " + name); property = new L2Property(template, up); properties.add(property); } if (structName != null && !"Vector".equals(structName) && !"Rotator".equals(structName) && !"Color".equals(structName)) { StructProperty structProperty = (StructProperty) property.getTemplate(); structName = structProperty.getStructType().getObjectFullName(); } UnrealPackageReadOnly.ExportEntry arrayInner = null; if (propertyType.equals(Type.ARRAY)) { ArrayProperty arrayProperty = (ArrayProperty) property.getTemplate(); arrayInner = (UnrealPackageReadOnly.ExportEntry) arrayProperty.getInner(); } DataInput objBuffer = new DataInputStream(new ByteArrayInputStream(objBytes), dataInput.getCharset()); property.putAt( arrayIndex, read( objBuffer, propertyType, array, arrayInner, structName, up, objClass, property.getName())); property.setType(propertyType); } } catch (IOException e) { throw new UnrealException(e); } return properties; }
public StompFrame unmarshal(DataInput in) throws IOException, ProtocolException { String action = null; // skip white space to next real action line while (true) { action = readLine(in, MAX_COMMAND_LENGTH, "The maximum command length was exceeded"); if (action == null) { throw new IOException("connection was closed"); } else { action = action.trim(); if (action.length() > 0) { break; } } } // Parse the headers Map<String, String> headers = new LinkedHashMap<String, String>(10); while (true) { String line = readLine(in, MAX_HEADER_LENGTH, "The maximum header length was exceeded"); if (line != null && line.trim().length() > 0) { if (headers.size() > MAX_HEADERS) { throw new ProtocolException("The maximum number of headers was exceeded", true); } try { int seperator_index = line.indexOf(Stomp.Headers.SEPERATOR); String name = line.substring(0, seperator_index).trim(); String value = line.substring(seperator_index + 1, line.length()).trim(); headers.put(name, value); } catch (Exception e) { throw new ProtocolException("Unable to parser header line [" + line + "]", true); } } else { break; } } // Read in the data part. byte[] data = NO_DATA; String contentLength = (String) headers.get(Stomp.Headers.CONTENT_LENGTH); if (contentLength != null) { // Bless the client, he's telling us how much data to read in. int length; try { length = Integer.parseInt(contentLength.trim()); } catch (NumberFormatException e) { throw new ProtocolException("Specified content-length is not a valid integer", true); } if (length > MAX_DATA_LENGTH) { throw new ProtocolException("The maximum data length was exceeded", true); } data = new byte[length]; in.readFully(data); if (in.readByte() != 0) { throw new ProtocolException( Stomp.Headers.CONTENT_LENGTH + " bytes were read and " + "there was no trailing null byte", true); } } else { // We don't know how much to read.. data ends when we hit a 0 byte b; ByteArrayOutputStream baos = null; while ((b = in.readByte()) != 0) { if (baos == null) { baos = new ByteArrayOutputStream(); } else if (baos.size() > MAX_DATA_LENGTH) { throw new ProtocolException("The maximum data length was exceeded", true); } baos.write(b); } if (baos != null) { baos.close(); data = baos.toByteArray(); } } return new StompFrame(action, headers, data); }
/** * Reads from the stream <code>in</code> a representation of a Unicode character string encoded in * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format; this string of characters is * then returned as a <code>String</code>. The details of the modified UTF-8 representation are * exactly the same as for the <code>readUTF</code> method of <code>DataInput</code>. * * @param in a data input stream. * @return a Unicode string. * @exception EOFException if the input stream reaches the end before all the bytes. * @exception IOException if an I/O error occurs. * @exception UTFDataFormatException if the bytes do not represent a valid modified UTF-8 encoding * of a Unicode string. * @see java.io.DataInputStream#readUnsignedShort() */ public static final String readUTF(DataInput in) throws IOException { int utflen = in.readUnsignedShort(); byte[] bytearr = null; char[] chararr = null; if (in instanceof DataInputStream) { DataInputStream dis = (DataInputStream) in; if (dis.bytearr.length < utflen) { dis.bytearr = new byte[utflen * 2]; dis.chararr = new char[utflen * 2]; } chararr = dis.chararr; bytearr = dis.bytearr; } else { bytearr = new byte[utflen]; chararr = new char[utflen]; } int c, char2, char3; int count = 0; int chararr_count = 0; in.readFully(bytearr, 0, utflen); while (count < utflen) { c = (int) bytearr[count] & 0xff; if (c > 127) break; count++; chararr[chararr_count++] = (char) c; } while (count < utflen) { c = (int) bytearr[count] & 0xff; switch (c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: /* 0xxxxxxx*/ count++; chararr[chararr_count++] = (char) c; break; case 12: case 13: /* 110x xxxx 10xx xxxx*/ count += 2; if (count > utflen) throw new UTFDataFormatException("malformed input: partial character at end"); char2 = (int) bytearr[count - 1]; if ((char2 & 0xC0) != 0x80) throw new UTFDataFormatException("malformed input around byte " + count); chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14: /* 1110 xxxx 10xx xxxx 10xx xxxx */ count += 3; if (count > utflen) throw new UTFDataFormatException("malformed input: partial character at end"); char2 = (int) bytearr[count - 2]; char3 = (int) bytearr[count - 1]; if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) throw new UTFDataFormatException("malformed input around byte " + (count - 1)); chararr[chararr_count++] = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; default: /* 10xx xxxx, 1111 xxxx */ throw new UTFDataFormatException("malformed input around byte " + count); } } // The number of chars produced may be less than utflen return new String(chararr, 0, chararr_count); }
/** * Deserialize. * * @param in the DataInput stream * @throws IOException Signals that an I/O exception has occurred. */ public void deserialize(DataInput in) throws IOException { this.clear(); final byte[] buffer4 = new byte[4]; final byte[] buffer = new byte[2]; // little endian in.readFully(buffer4); final int cookie = (buffer4[0] & 0xFF) | ((buffer4[1] & 0xFF) << 8) | ((buffer4[2] & 0xFF) << 16) | ((buffer4[3] & 0xFF) << 24); if (cookie != SERIAL_COOKIE) throw new IOException("I failed to find the right cookie."); in.readFully(buffer4); this.size = (buffer4[0] & 0xFF) | ((buffer4[1] & 0xFF) << 8) | ((buffer4[2] & 0xFF) << 16) | ((buffer4[3] & 0xFF) << 24); if ((this.array == null) || (this.array.length < this.size)) this.array = new Element[this.size]; final short keys[] = new short[this.size]; final int cardinalities[] = new int[this.size]; final boolean isBitmap[] = new boolean[this.size]; for (int k = 0; k < this.size; ++k) { in.readFully(buffer); keys[k] = (short) (buffer[0] & 0xFF | ((buffer[1] & 0xFF) << 8)); in.readFully(buffer); cardinalities[k] = 1 + (buffer[0] & 0xFF | ((buffer[1] & 0xFF) << 8)); isBitmap[k] = cardinalities[k] > ArrayContainer.DEFAULT_MAX_SIZE; } for (int k = 0; k < this.size; ++k) { Container val; if (isBitmap[k]) { final LongBuffer bitmapArray = LongBuffer.allocate(BitmapContainer.MAX_CAPACITY / 64); final byte[] buf = new byte[8]; // little endian for (int l = 0; l < bitmapArray.limit(); ++l) { in.readFully(buf); bitmapArray.put( l, (((long) buf[7] << 56) + ((long) (buf[6] & 255) << 48) + ((long) (buf[5] & 255) << 40) + ((long) (buf[4] & 255) << 32) + ((long) (buf[3] & 255) << 24) + ((buf[2] & 255) << 16) + ((buf[1] & 255) << 8) + (buf[0] & 255))); } val = new BitmapContainer(bitmapArray, cardinalities[k]); } else { final ShortBuffer shortArray = ShortBuffer.allocate(cardinalities[k]); for (int l = 0; l < shortArray.limit(); ++l) { in.readFully(buffer); shortArray.put(l, (short) (buffer[0] & 0xFF | ((buffer[1] & 0xFF) << 8))); } val = new ArrayContainer(shortArray, cardinalities[k]); } this.array[k] = new Element(keys[k], val); } }
@Override public void readFields(DataInput in) throws IOException { /* * extract pkt len. * * GPSQL-1107: * The DataInput might already be empty (EOF), but we can't check it beforehand. * If that's the case, pktlen is updated to -1, to mark that the object is still empty. * (can be checked with isEmpty()). */ pktlen = readPktLen(in); if (isEmpty()) { return; } /* extract the version and col cnt */ int version = in.readShort(); int curOffset = 4 + 2; int colCnt; /* !!! Check VERSION !!! */ if (version != GPDBWritable.VERSION && version != GPDBWritable.PREV_VERSION) { throw new IOException( "Current GPDBWritable version(" + GPDBWritable.VERSION + ") does not match input version(" + version + ")"); } if (version == GPDBWritable.VERSION) { errorFlag = in.readByte(); curOffset += 1; } colCnt = in.readShort(); curOffset += 2; /* Extract Column Type */ colType = new int[colCnt]; DBType[] coldbtype = new DBType[colCnt]; for (int i = 0; i < colCnt; i++) { int enumType = (in.readByte()); curOffset += 1; if (enumType == DBType.BIGINT.ordinal()) { colType[i] = BIGINT.getOID(); coldbtype[i] = DBType.BIGINT; } else if (enumType == DBType.BOOLEAN.ordinal()) { colType[i] = BOOLEAN.getOID(); coldbtype[i] = DBType.BOOLEAN; } else if (enumType == DBType.FLOAT8.ordinal()) { colType[i] = FLOAT8.getOID(); coldbtype[i] = DBType.FLOAT8; } else if (enumType == DBType.INTEGER.ordinal()) { colType[i] = INTEGER.getOID(); coldbtype[i] = DBType.INTEGER; } else if (enumType == DBType.REAL.ordinal()) { colType[i] = REAL.getOID(); coldbtype[i] = DBType.REAL; } else if (enumType == DBType.SMALLINT.ordinal()) { colType[i] = SMALLINT.getOID(); coldbtype[i] = DBType.SMALLINT; } else if (enumType == DBType.BYTEA.ordinal()) { colType[i] = BYTEA.getOID(); coldbtype[i] = DBType.BYTEA; } else if (enumType == DBType.TEXT.ordinal()) { colType[i] = TEXT.getOID(); coldbtype[i] = DBType.TEXT; } else { throw new IOException("Unknown GPDBWritable.DBType ordinal value"); } } /* Extract null bit array */ byte[] nullbytes = new byte[getNullByteArraySize(colCnt)]; in.readFully(nullbytes); curOffset += nullbytes.length; boolean[] colIsNull = byteArrayToBooleanArray(nullbytes, colCnt); /* extract column value */ colValue = new Object[colCnt]; for (int i = 0; i < colCnt; i++) { if (!colIsNull[i]) { /* Skip the alignment padding */ int skipbytes = roundUpAlignment(curOffset, coldbtype[i].getAlignment()) - curOffset; for (int j = 0; j < skipbytes; j++) { in.readByte(); } curOffset += skipbytes; /* For fixed length type, increment the offset according to type type length here. * For var length type (BYTEA, TEXT), we'll read 4 byte length header and the * actual payload. */ int varcollen = -1; if (coldbtype[i].isVarLength()) { varcollen = in.readInt(); curOffset += 4 + varcollen; } else { curOffset += coldbtype[i].getTypeLength(); } switch (DataType.get(colType[i])) { case BIGINT: { colValue[i] = in.readLong(); break; } case BOOLEAN: { colValue[i] = in.readBoolean(); break; } case FLOAT8: { colValue[i] = in.readDouble(); break; } case INTEGER: { colValue[i] = in.readInt(); break; } case REAL: { colValue[i] = in.readFloat(); break; } case SMALLINT: { colValue[i] = in.readShort(); break; } /* For BYTEA column, it has a 4 byte var length header. */ case BYTEA: { colValue[i] = new byte[varcollen]; in.readFully((byte[]) colValue[i]); break; } /* For text formatted column, it has a 4 byte var length header * and it's always null terminated string. * So, we can remove the last "\0" when constructing the string. */ case TEXT: { byte[] data = new byte[varcollen]; in.readFully(data, 0, varcollen); colValue[i] = new String(data, 0, varcollen - 1, CHARSET); break; } default: throw new IOException("Unknown GPDBWritable ColType"); } } } /* Skip the ending alignment padding */ int skipbytes = roundUpAlignment(curOffset, 8) - curOffset; for (int j = 0; j < skipbytes; j++) { in.readByte(); } curOffset += skipbytes; if (errorFlag != 0) { throw new IOException("Received error value " + errorFlag + " from format"); } }
public String readString(int length) throws IOException { DataInput reader = new DataInputStream(inputStream); byte[] responseBuffer = new byte[length]; reader.readFully(responseBuffer); return new String(responseBuffer, Charset.forName("utf-8")); }
public void readFrom(DataInput in) throws Exception { type = in.readByte(); short len = in.readShort(); version = new byte[len]; in.readFully(version); }