public static String readUTF()

in utils/src/main/java/jetbrains/exodus/util/UTFUtil.java [67:136]


    public static String readUTF(@NotNull final InputStream stream) throws IOException {
        final DataInputStream dataInput = new DataInputStream(stream);
        if (stream instanceof ByteArraySizedInputStream) {
            final ByteArraySizedInputStream sizedStream = (ByteArraySizedInputStream) stream;
            final int streamSize = sizedStream.size();
            if (streamSize >= 2) {
                sizedStream.mark(Integer.MAX_VALUE);
                try {
                    final int utfLen = dataInput.readUnsignedShort();
                    if (utfLen == streamSize - 2) {
                        boolean isAscii = true;
                        final byte[] bytes = sizedStream.toByteArray();
                        for (int i = 0; i < utfLen; ++i) {
                            if ((bytes[i + 2] & 0xff) > 127) {
                                isAscii = false;
                                break;
                            }
                        }
                        if (isAscii) {
                            return fromAsciiByteArray(bytes, 2, utfLen);
                        }
                    }
                } finally {
                    sizedStream.reset();
                }
            }
        }

        try (dataInput) {
            //streams managed by transaction should be reset.
            if (stream.markSupported()) {
                stream.mark(Integer.MAX_VALUE);
            }

            String result = null;
            StringBuilder builder = null;
            for (; ; ) {
                final String temp;
                try {
                    temp = dataInput.readUTF();
                    if (result != null && result.length() == 0 &&
                            builder != null && builder.length() == 0 && temp.length() == 0) {
                        break;
                    }
                } catch (EOFException e) {
                    break;
                }
                if (result == null) {
                    result = temp;
                } else {
                    if (builder == null) {
                        builder = new StringBuilder();
                        builder.append(result);
                    }
                    builder.append(temp);
                }
            }


            if (stream.markSupported()) {
                try {
                    stream.reset();
                } catch (IOException e) {
                    //should never happen with tx managed steams
                }
            }

            return (builder != null) ? builder.toString() : result;
        }
    }