private int readSafely(@NotNull InputStream stream, byte @NotNull [] buffer, int length) throws IOException {
    int n = stream.read(buffer, 0, length);
    if (n <= 0) {
      // maybe locked because someone else is writing to it
      // repeat inside read action to guarantee all writes are finished
      if (toLog()) {
        log("F: readSafely(): inputStream.read(" +length+ ") returned "+n+"; retrying with read action. stream="+ streamInfo(stream));
      }
      n = stream.read(buffer, 0, length);
      if (toLog()) {
        log("F: readSafely(): under read action inputStream.read(" +length+ ") returned "+n+"; stream="+ streamInfo(stream));
      }
    }
    return n;
  }
private int readSafely(@NotNull InputStream stream, byte @NotNull [] buffer, int length)