added output buffer length detect