/** * Repeated reads until the buffer is full or IOException is thrown * * @return number of bytes read (should always equal target.length) */ protected int read(InputStream in, byte target[]) throws IOException { return DataHelper.read(in, target); } }
/** * Same as DataHelper.readString, except that * strings up to 4K bytes are allowed. * Format is: one-byte length + data, or 0xff + two-byte length + data * * @param in stream to read from * @throws DataFormatException if the stream doesn't contain a validly formatted string * @throws EOFException if there aren't enough bytes to read the string * @throws IOException if there is an IO error reading the string * @return UTF-8 string */ private static String readLongString(ByteArrayInputStream in) throws DataFormatException, IOException { int size = in.read(); if (size < 0) throw new EOFException("EOF reading string"); if (size == 0xff) { size = (int) DataHelper.readLong(in, 2); if (size > MAX_VALUE_LENGTH) throw new DataFormatException(MAX_VALUE_LENGTH + " max, but this is " + size); } if (size == 0) return ""; byte raw[] = new byte[size]; int read = DataHelper.read(in, raw); if (read != size) throw new EOFException("EOF reading string"); return new String(raw, "UTF-8"); }
/** * This is different than InputStream.read(target), in that it * does repeated reads until the full data is received. * * As of 0.9.27, throws EOFException if the full length is not read. * * @return target.length * @throws EOFException if the full length is not read (since 0.9.27) */ public static int read(InputStream in, byte target[]) throws IOException { return read(in, target, 0, target.length); }
/** * If null, P256 key, or Ed25519 key cert, return immutable static instance, else create new * @since 0.8.3 */ public static Certificate create(InputStream in) throws DataFormatException, IOException { // EOF will be thrown in next read int type = in.read(); int length = (int) DataHelper.readLong(in, 2); if (type == 0 && length == 0) return NULL_CERT; // from here down roughly the same as readBytes() below if (length == 0) return new Certificate(type, null); byte[] payload = new byte[length]; int read = DataHelper.read(in, payload); if (read != length) throw new DataFormatException("Not enough bytes for the payload (read: " + read + " length: " + length + ')'); if (type == CERTIFICATE_TYPE_KEY) { if (length == 4) { if (Arrays.equals(payload, KeyCertificate.Ed25519_PAYLOAD)) return KeyCertificate.ELG_Ed25519_CERT; if (Arrays.equals(payload, KeyCertificate.ECDSA256_PAYLOAD)) return KeyCertificate.ELG_ECDSA256_CERT; } return new KeyCertificate(payload); } return new Certificate(type, payload); }
/** * Start the LookaheadInputStream with the given input stream. * Resets everything if the LookaheadInputStream was previously used. * WARNING - blocking until lookaheadSize bytes are read! * * @throws IOException if less than lookaheadSize bytes could be read. */ public void initialize(InputStream src) throws IOException { in = src; _eofReached = false; index = 0; DataHelper.read(in, _footerLookahead); }
/** * This does NOT validate the signature * * @throws IllegalStateException if called more than once or Destination already set */ @Override public void readBytes(InputStream in) throws DataFormatException, IOException { if (_signingKey != null) throw new IllegalStateException(); // LS2 header readHeader(in); // Encrypted LS2 part int encryptedSize = (int) DataHelper.readLong(in, 2); if (encryptedSize < MIN_ENCRYPTED_SIZE || encryptedSize > MAX_ENCRYPTED_SIZE) throw new DataFormatException("bad LS size: " + encryptedSize); _encryptedData = new byte[encryptedSize]; DataHelper.read(in, _encryptedData); // signature type depends on offline or not SigType type = isOffline() ? _transientSigningPublicKey.getType() : _signingKey.getType(); _signature = new Signature(type); _signature.readBytes(in); }
/** Read in a string from the stream as specified by the I2P data structure spec. * A string is 1 or more bytes where the first byte is the number of bytes (not characters!) * in the string and the remaining 0-255 bytes are the non-null terminated UTF-8 encoded character array. * * @param in stream to read from * @throws DataFormatException if the stream doesn't contain a validly formatted string * @throws EOFException since 0.8.2, if there aren't enough bytes to read the string * @throws IOException if there is an IO error reading the string * @return UTF-8 string */ public static String readString(InputStream in) throws DataFormatException, IOException { int size = in.read(); if (size == -1) throw new EOFException("EOF reading string"); if (size == 0) return ""; // reduce object proliferation size &= 0xff; byte raw[] = new byte[size]; // full read guaranteed read(in, raw); // the following constructor throws an UnsupportedEncodingException which is an IOException, // but that's only if UTF-8 is not supported. Other encoding errors are not thrown. return new String(raw, "UTF-8"); }
/** * Read ahead to the infohash, throwing an exception if there isn't enough data. * Also check the first 20 bytes for the correct protocol here and throw IOE if bad, * so we don't hang waiting for 48 bytes if it's not a bittorrent client. * The 20 bytes are checked again in Peer.handshake(). */ private static byte[] readHash(InputStream in) throws IOException { for (int i = 0; i < PROTO_LEN; i++) { int b = in.read(); if (b != PROTO[i]) throw new IOException("Bad protocol 0x" + Integer.toHexString(b) + " at byte " + i); } DataHelper.skip(in, 8); byte buf[] = new byte[20]; int read = DataHelper.read(in, buf); if (read != buf.length) throw new IOException("Unable to read the hash (read " + read + ")"); return buf; }
public V get(InputStream in) throws IOException { byte[] data = SimpleByteCache.acquire(_datalen); int read = DataHelper.read(in, data); if (read != _datalen) throw new EOFException("Not enough bytes to read the data"); return get(data); }
DataHelper.skip(inputStream, Signature.SIGNATURE_BYTES); byte[] data = new byte[VERSION_BYTES]; int bytesRead = DataHelper.read(inputStream, data);
DataHelper.skip(fileInputStream, Signature.SIGNATURE_BYTES); byte[] data = new byte[VERSION_BYTES]; int bytesRead = DataHelper.read(fileInputStream, data);
/** * Send bytes through the SAM STREAM session socket sender * * @throws IOException if the client didnt provide enough data */ @Override public void sendBytes(InputStream in, int size) throws IOException { if (_log.shouldLog(Log.DEBUG)) _log.debug("Handler " + _id + ": sending " + size + " bytes"); ByteArray ba = _cache.acquire(); int read = DataHelper.read(in, ba.getData(), 0, size); if (read != size) throw new IOException("Insufficient data from the SAM client (" + read + "/" + size + ")"); ba.setValid(read); synchronized (_data) { _data.add(ba); _data.notifyAll(); } }
DataHelper.skip(in, skip); byte[] hdr = new byte[HEADER_LEN]; DataHelper.read(in, hdr); if (!DataHelper.eq(hdr, magicStart)) throw new ZipException("Not a zip file: " + file); DataHelper.skip(in, fileLen - (skip + HEADER_LEN + buffer.length)); DataHelper.read(in, buffer); return getComment(buffer); } finally {
in = p.getInputStream(); byte[] data = new byte[(int) len]; DataHelper.read(in, data); String enc = httpRequest.getCharacterEncoding(); if (enc == null)
fin = new FileInputStream(_destFile); byte dest[] = new byte[1024]; int read = DataHelper.read(fin, dest);
int read = DataHelper.read ( in, ba.getData(), 0, size );
FileInputStream fin = new FileInputStream(_peerDestFiles[i]); byte dest[] = new byte[1024]; int read = DataHelper.read(fin, dest);
@Test public void testRandomOrder() throws IOException { byte orig[] = new byte[256*1024]; _context.random().nextBytes(orig); ArrayList<Integer> order = new ArrayList<Integer>(32); for (int i = 0; i < orig.length / 1024; i++) order.add(new Integer(i)); Collections.shuffle(order); for (int i = 0; i < orig.length / 1024; i++) { byte msg[] = new byte[1024]; Integer cur = (Integer)order.get(i); System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024); in.messageReceived(cur.intValue(), new ByteArray(msg)); _log.debug("Injecting " + cur); } byte read[] = new byte[orig.length]; int howMany = DataHelper.read(in, read); if (howMany != orig.length) fail("not enough bytes read [" + howMany + "]"); if (!DataHelper.eq(orig, read)) fail("data read is not equal"); _log.info("Passed test: random order"); }
@Test public void testRandomDups() throws IOException { byte orig[] = new byte[256*1024]; _context.random().nextBytes(orig); for (int n = 0; n < 3; n++) { ArrayList<Integer> order = new ArrayList<Integer>(32); for (int i = 0; i < orig.length / 1024; i++) order.add(new Integer(i)); Collections.shuffle(order); for (int i = 0; i < orig.length / 1024; i++) { byte msg[] = new byte[1024]; Integer cur = (Integer)order.get(i); System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024); in.messageReceived(cur.intValue(), new ByteArray(msg)); _log.debug("Injecting " + cur); } } byte read[] = new byte[orig.length]; int howMany = DataHelper.read(in, read); if (howMany != orig.length) fail("not enough bytes read [" + howMany + "]"); if (!DataHelper.eq(orig, read)) fail("data read is not equal"); _log.info("Passed test: random dups"); }
@Test public void testInOrder() throws IOException { byte orig[] = new byte[256*1024]; _context.random().nextBytes(orig); for (int i = 0; i < orig.length / 1024; i++) { byte msg[] = new byte[1024]; System.arraycopy(orig, i*1024, msg, 0, 1024); in.messageReceived(i, new ByteArray(msg)); } byte read[] = new byte[orig.length]; int howMany = DataHelper.read(in, read); if (howMany != orig.length) fail("not enough bytes read [" + howMany + "]"); if (!DataHelper.eq(orig, read)) fail("data read is not equal"); _log.info("Passed test: in order"); }