Source:
<code>public void run() { if (mMeasurement.finishTime > 0) { mCountDownLatch.countDown(); return; } try { setupSSLSocket(); mMeasurement.startTime = now(); connectAndHandshake(); } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { mMeasurement.recordFailure(e.toString()); return; } if (!TextUtils.isEmpty(mHostname)) { if (!verifyHostname(mHostname)) { mMeasurement.recordFailure("Hostname verification failed"); return; } } mMeasurement.description += " src{" + getLocalAddressString(mSslSocket) + "}"; final String sixRandomDigits = String.valueOf(mRandom.nextInt(900000) + 100000); mMeasurement.description += " qtype{" + mQueryType + "}" + " qname{" + sixRandomDigits + "-android-ds.metric.gstatic.com}"; final byte[] dnsPacket = getDnsQueryPacket(sixRandomDigits); try { OutputStream output = mSslSocket.getOutputStream(); <START> byte[] twoBytesHeader = {0, (byte) dnsPacket.length}; output.write(twoBytesHeader); <END> output.write(dnsPacket); InputStream input = mSslSocket.getInputStream(); int len = input.read(twoBytesHeader, 0, 2); final short expectReplyLen = ByteBuffer.wrap(twoBytesHeader).getShort(); byte[] reply = new byte[expectReplyLen]; int read = 0; while (read < expectReplyLen) { int n = input.read(reply, read, expectReplyLen - read); if (n <= 0) break; read += n; } final String rcodeStr = (read == expectReplyLen) ? " " + responseCodeStr((int) (reply[3]) & 0x0f) : ""; mMeasurement.recordSuccess("1/1" + rcodeStr); } catch (IOException e) { mMeasurement.recordFailure(e.toString()); } try { mSslSocket.close(); } catch (IOException ignored) { } }</code><technical_language>This is a header, a length, right? If so, suggest assuming length is < 256. about: ByteBuffer header = ByteBuffer.allocate(2); header.putShort((short) dnsPacket.length); output.write(header.array());</technical_language>

Target:
public void run() { if (mMeasurement.finishTime > 0) { mCountDownLatch.countDown(); return; } try { setupSSLSocket(); mMeasurement.startTime = now(); connectAndHandshake(); } catch (NoSuchAlgorithmException | IOException | KeyManagementException | KeyStoreException e) { mMeasurement.recordFailure(e.toString()); return; } if (!TextUtils.isEmpty(mHostname)) { if (!verifyHostname(mHostname)) { mMeasurement.recordFailure("Hostname verification failed"); return; } } mMeasurement.description += " src{" + getLocalAddressString(mSslSocket) + "}"; final String sixRandomDigits = String.valueOf(mRandom.nextInt(900000) + 100000); mMeasurement.description += " qtype{" + mQueryType + "}" + " qname{" + sixRandomDigits + "-android-ds.metric.gstatic.com}"; final byte[] dnsPacket = getDnsQueryPacket(sixRandomDigits); try { OutputStream output = mSslSocket.getOutputStream(); ByteBuffer querySize = ByteBuffer.allocate(2); querySize.putShort((short) dnsPacket.length); output.write(querySize.array()); output.write(dnsPacket); InputStream input = mSslSocket.getInputStream(); byte[] buf = new byte[2]; input.read(buf, 0, 2); final short expectReplyLen = ByteBuffer.wrap(buf).getShort(); byte[] reply = new byte[expectReplyLen]; int read = 0; while (read < expectReplyLen) { int n = input.read(reply, read, expectReplyLen - read); if (n < 0) break; read += n; } if (read == expectReplyLen && read > DNS_HEADER_SIZE) { mMeasurement.recordSuccess("1/1 " + responseCodeStr((int) (reply[3]) & 0x0f)); } else { mMeasurement.recordFailure("Unexpected answer length " + read); } } catch (IOException e) { mMeasurement.recordFailure(e.toString()); } try { mSslSocket.close(); } catch (IOException ignored) { } }