/** Write a {@code string} field to the stream. */ // TODO(dweis): Document behavior on ill-formed UTF-16 input. public void writeStringNoTag(final String value) throws IOException { try { efficientWriteStringNoTag(value); } catch (UnpairedSurrogateException e) { logger.log(Level.WARNING, "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", e); inefficientWriteStringNoTag(value); } }
/** * Compute the number of bytes that would be needed to encode a * {@code string} field. */ public static int computeStringSizeNoTag(final String value) { int length; try { length = Utf8.encodedLength(value); } catch (UnpairedSurrogateException e) { // TODO(dweis): Consider using nio Charset methods instead. final byte[] bytes = value.getBytes(Internal.UTF_8); length = bytes.length; } return computeRawVarint32Size(length) + length; }
/** * Compute the number of bytes that would be needed to encode a * {@code string} field. */ public static int computeStringSizeNoTag(final String value) { int length; try { length = Utf8.encodedLength(value); } catch (UnpairedSurrogateException e) { // TODO(dweis): Consider using nio Charset methods instead. final byte[] bytes = value.getBytes(Internal.UTF_8); length = bytes.length; } return computeLengthDelimitedFieldSize(length); }