/** * Write a file with the content assuming UTF8. * * @param content The content * @param file The file to write * @throws IOException e */ public static void writeFile(String content, File file) throws IOException { FileOutputStream outputStream = new FileOutputStream(file); FileChannel fc = outputStream.getChannel(); ByteBuffer buffer = ByteBuffer.wrap(Utf8.toBytes(content)); fc.write(buffer); outputStream.close(); }
/** * Read a file and return the content as string assuming UTF8. * * @param file The file to read * @return The content * @throws IOException e */ public static String readFile(File file) throws IOException { long len = 0; if (file.length() >= Integer.MAX_VALUE) { throw new IOException("Too large file, unexpected!"); } else { len = file.length(); } byte[] buf = new byte[(int) len]; InputStream is = new FileInputStream(file); readInputStream(is, buf); return Utf8.toString(buf); }
/** * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. * * <p>When encoding the line length and line separator are given in the constructor, and the * encoding table is STANDARD_ENCODE_TABLE. * * <p>Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 * in the encoded data. * * <p>When decoding all variants are supported. * * @param lineLength Each line of encoded data will be at most of the given length (rounded down * to nearest multiple of 4). If {@code lineLength <= 0}, then the output will not be divided * into lines (chunks). Ignored when decoding. * @param lineSeparator Each line of encoded data will end with this sequence of bytes. * @param urlSafe Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is * only applied to encode operations. Decoding seamlessly handles both modes. * @throws IllegalArgumentException The provided lineSeparator included some base64 characters. * That's not going to work! * @since 1.4 */ public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) { if (lineSeparator == null) { lineLength = 0; // disable chunk-separating lineSeparator = CHUNK_SEPARATOR; // this just gets ignored } this.lineLength = lineLength > 0 ? (lineLength / 4) * 4 : 0; this.lineSeparator = new byte[lineSeparator.length]; System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length); if (lineLength > 0) { this.encodeSize = 4 + lineSeparator.length; } else { this.encodeSize = 4; } this.decodeSize = this.encodeSize - 1; if (containsBase64Byte(lineSeparator)) { String sep = Utf8.toString(lineSeparator); throw new IllegalArgumentException( "lineSeperator must not contain base64 characters: [" + sep + "]"); } this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE; }
/** * Read a {@code string} field value from the stream. If the stream contains malformed UTF-8, * throw exception {@link InvalidProtocolBufferException}. */ public String readStringRequireUtf8() throws IOException { final int size = readRawVarint32(); final byte[] bytes; int pos = bufferPos; if (size <= (bufferSize - pos) && size > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. bytes = buffer; bufferPos = pos + size; } else if (size == 0) { return ""; } else { // Slow path: Build a byte array first then copy it. bytes = readRawBytesSlowPath(size); pos = 0; } // TODO(martinrb): We could save a pass by validating while decoding. if (!Utf8.isValidUtf8(bytes, pos, pos + size)) { throw InvalidProtocolBufferException.invalidUtf8(); } return new String(bytes, pos, size, Internal.UTF_8); }
/** * Encodes a byte[] containing binary data, into a String containing characters in the Base64 * alphabet. * * @param pArray a byte array containing binary data * @return A String containing only Base64 character data * @since 1.4 */ public String encodeToString(byte[] pArray) { return Utf8.toString(encode(pArray)); }
/** * Decodes a String containing containing characters in the Base64 alphabet. * * @param pArray A String containing Base64 character data * @return a byte array containing binary data * @since 1.4 */ public byte[] decode(String pArray) { return decode(Utf8.toBytes(pArray)); }
/** * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the * output. The url-safe variation emits - and _ instead of + and / characters. * * @param binaryData binary data to encode * @return String containing Base64 characters * @since 1.4 */ public static String encodeBase64URLSafeString(byte[] binaryData) { return Utf8.toString(encodeBase64(binaryData, false, true)); }
/** * Encodes binary data using the base64 algorithm into 76 character blocks separated by CRLF. * * @param binaryData binary data to encode * @return String containing Base64 characters. * @since 1.4 */ public static String encodeBase64String(byte[] binaryData) { return Utf8.toString(encodeBase64(binaryData, true)); }
@Override protected int partialIsValidUtf8(int state, int offset, int length) { return Utf8.partialIsValidUtf8(state, buffer, offset, offset + length); }
@Override public boolean isValidUtf8() { return Utf8.isValidUtf8(buffer); }
/** * Read an input stream and return the content as string assuming UTF8. * * @param in The input stream * @return The content * @throws IOException e */ public static String readInput(InputStream in) throws IOException { byte[] content = readInputStream(in); return Utf8.toString(content); }
@Override protected final int partialIsValidUtf8(int state, int offset, int length) { int index = getOffsetIntoBytes() + offset; return Utf8.partialIsValidUtf8(state, bytes, index, index + length); }
@Override public final boolean isValidUtf8() { int offset = getOffsetIntoBytes(); return Utf8.isValidUtf8(bytes, offset, offset + size()); }
/** Like {@link #isValidUtf8(ByteString)} but for byte arrays. */ public static boolean isValidUtf8(byte[] byteArray) { return Utf8.isValidUtf8(byteArray); }