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