@@ -111,9 +111,28 @@ protected void debugStream(byte[] array, int position, int remaining) {}
111111
112112 protected MT readMessageType (@ Nonnull Memento memento ) throws IOException , MalformedFrameException {
113113 byte [] header = getEncodedMagicNumber ();
114- int readCount = DELIMITER_LENGTH + header .length + DELIMITER_LENGTH + BYTE_LENGTH + DELIMITER_LENGTH ;
115- read (memento , readCount );
116- checkHeader (memento );
114+ // Read the header one byte at a time so that non-protocol output (e.g. the JDWP
115+ // "Listening for transport dt_socket at address: ..." line) is flushed to the console
116+ // before the decoder blocks. That line contains ':' which looks like a frame start;
117+ // checking each magic-number byte individually lets us bail out immediately on a
118+ // mismatch instead of stalling on a 24-byte read that never completes.
119+ read (memento , DELIMITER_LENGTH );
120+ ByteBuffer bb = memento .getByteBuffer ();
121+ if ((bb .array ()[bb .arrayOffset () + bb .position ()] & 0xff ) != ':' ) {
122+ checkHeader (memento ); // not ':', immediately throws MalformedFrameException
123+ }
124+ checkDelimiter (memento ); // consume the ':'
125+ for (int i = 0 ; i < header .length ; i ++) {
126+ read (memento , DELIMITER_LENGTH );
127+ bb = memento .getByteBuffer ();
128+ if ((bb .array ()[bb .arrayOffset () + bb .position ()] & 0xff ) != (header [i ] & 0xff )) {
129+ // Mismatch: report the ':' plus all bytes read so far in this frame attempt.
130+ throw new MalformedFrameException (memento .getLine ().getPositionByteBuffer (), bb .position () + 1 );
131+ }
132+ bb .position (bb .position () + 1 );
133+ }
134+ read (memento , DELIMITER_LENGTH );
135+ checkDelimiter (memento );
117136 return messageTypes .get (readSegment (memento ));
118137 }
119138
0 commit comments