List of usage examples for java.io PushbackInputStream read
public int read(byte[] b, int off, int len) throws IOException
len
bytes of data from this input stream into an array of bytes. From source file:org.openymsg.network.HTTPConnectionHandler.java
/** * The only time Yahoo can actually send us any packets is when we send it some. Yahoo encodes its packets in a POST * body - the format is the same binary representation used for direct connections (with one or two extra codes). * //from www . jav a2s.c o m * After posting a packet, the connection will receive a HTTP response who's payload consists of four bytes followed * by zero or more packets. The first byte of the four is a count of packets encoded in the following body. * * Each incoming packet is transfered to a queue, where receivePacket() takes them off - thus preserving the effect * that input and output packets are being received independently, as with other connection handlers. As * readPacket() can throw an exception, these are caught and transfered onto the queue too, then rethrown by * receivePacket() . */ @Override synchronized void sendPacket(PacketBodyBuffer body, ServiceType service, long status, long sessionID) throws IOException, IllegalStateException { if (!connected) throw new IllegalStateException("Not logged in"); if (filterOutput(body, service)) return; byte[] b = body.getBuffer(); Socket soc = new Socket(proxyHost, proxyPort); PushbackInputStream pbis = new PushbackInputStream(soc.getInputStream()); DataOutputStream dos = new DataOutputStream(soc.getOutputStream()); // HTTP header dos.writeBytes(HTTP_HEADER_POST); dos.writeBytes("Content-length: " + (b.length + NetworkConstants.YMSG9_HEADER_SIZE) + NetworkConstants.END); dos.writeBytes(HTTP_HEADER_AGENT); dos.writeBytes(HTTP_HEADER_HOST); if (HTTP_HEADER_PROXY_AUTH != null) dos.writeBytes(HTTP_HEADER_PROXY_AUTH); if (cookie != null) dos.writeBytes("Cookie: " + cookie + NetworkConstants.END); dos.writeBytes(NetworkConstants.END); // YMSG9 header dos.write(NetworkConstants.MAGIC, 0, 4); dos.write(NetworkConstants.VERSION_HTTP, 0, 4); dos.writeShort(b.length & 0xffff); dos.writeShort(service.getValue() & 0xffff); dos.writeInt((int) (status & 0xffffffff)); dos.writeInt((int) (sessionID & 0xffffffff)); // YMSG9 body dos.write(b, 0, b.length); dos.flush(); // HTTP response header String s = readLine(pbis); if (s == null || s.indexOf(" 200 ") < 0) // Not "HTTP/1.0 200 OK" { throw new IOException("HTTP request returned didn't return OK (200): " + s); } while (s != null && s.trim().length() > 0) // Read past header s = readLine(pbis); // Payload count byte[] code = new byte[4]; int res = pbis.read(code, 0, 4); // Packet count (Little-Endian?) if (res < 4) { throw new IOException("Premature end of HTTP data"); } int count = code[0]; // Payload body YMSG9InputStream yip = new YMSG9InputStream(pbis); YMSG9Packet pkt; for (int i = 0; i < count; i++) { pkt = yip.readPacket(); if (!filterInput(pkt)) { if (!packets.add(pkt)) { throw new IllegalArgumentException("Unable to add data to the packetQueue!"); } } } soc.close(); // Reset idle timeout lastFetch = System.currentTimeMillis(); }