📄 base64.java
字号:
} break; case 6:{ lineBreaks = false; } break; case 'g':{ action = ACTION_GUESS; } break; case 'x':{ extension = opts.getOptarg(); if (extension == null) extension = ""; } break; case 'f':{ force = true; } break; case 4:{ force = false; } break; case 'v':{ printMessages = true; printErrors = true; } break; case 'q':{ printMessages = false; printErrors = true; } break; case 'Q':{ printMessages = false; printErrors = false; } break; case 7: { argumentType = ARGUMENT_FILE; } break; case 8: { argumentType = ARGUMENT_STRING; } break; case 'n': { decodeEndLine = true; } break; case 9: { decodeEndLine = false; } break; default:{ System.err.println(labels.getString("unknownarg")); System.exit(1); } } } int exitCond = 0; boolean done = false; for (int i=opts.getOptind(); i<args.length; i++){ done = true; File source = new File(args[i]); if (argumentType == ARGUMENT_STRING || (argumentType == ARGUMENT_GUESS && !source.exists())){ try { int fileAction = action; if (fileAction == ACTION_GUESS){ if (isBase64(args[i])){ fileAction = ACTION_DECODE; } else { fileAction = ACTION_ENCODE; } } if (fileAction == ACTION_ENCODE){ if (printMessages){ System.out.println(labels.getString("encodingarg")); } encode(new ByteArrayInputStream(args[i].getBytes()), System.out, lineBreaks); } else { if (printMessages){ System.out.println(labels.getString("decodingarg")); } decode(new ByteArrayInputStream(args[i].getBytes()), System.out, !forceDecode); if (decodeEndLine) System.out.println(); } } catch (Base64DecodingException x){ if(printErrors){ System.err.println(args[i] + ": " + x.getMessage() + " " + labels.getString("unexpectedcharforce")); } exitCond = 1; } catch (IOException x){ if(printErrors){ System.err.println(args[i] + ": " + x.getMessage()); } exitCond = 1; } } else if (!source.exists()){ if(printErrors){ System.err.println(MessageFormat.format(labels.getString("doesnotexist"), new String[] {args[i]})); } exitCond = 1; } else if (!source.canRead()){ if(printErrors){ System.err.println(MessageFormat.format(labels.getString("cantread"), new String[] {args[i]})); } exitCond = 1; } else { try { int fileAction = action; if (fileAction == ACTION_GUESS){ if (isBase64(source)){ fileAction = ACTION_DECODE; } else { fileAction = ACTION_ENCODE; } } String outName = args[i]; if (extension.length() > 0){ if (fileAction == ACTION_ENCODE){ outName = args[i] + "." + extension; } else { if (args[i].endsWith("." + extension)){ outName = args[i].substring(0, args[i].length() - (extension.length() + 1)); } } } File outFile = new File(outName); if (!force && outFile.exists()){ if(printErrors){ System.err.println(MessageFormat.format(labels.getString("overwrite"), new String[] {outName})); } exitCond = 1; } else if (!(outFile.exists() || outFile.createNewFile()) || !outFile.canWrite()){ if(printErrors){ System.err.println(MessageFormat.format(labels.getString("cantwrite"), new String[] {outName})); } exitCond = 1; } else { if (fileAction == ACTION_ENCODE){ if (printMessages){ System.out.println(MessageFormat.format(labels.getString("encoding"), new String[] {args[i], outName})); } encode(source, outFile, lineBreaks); } else { if (printMessages){ System.out.println(MessageFormat.format(labels.getString("decoding"), new String[] {args[i], outName})); } decode(source, outFile, !forceDecode); } } } catch (Base64DecodingException x){ if(printErrors){ System.err.println(args[i] + ": " + x.getMessage() + " " + labels.getString("unexpectedcharforce")); } exitCond = 1; } catch (IOException x){ if(printErrors){ System.err.println(args[i] + ": " + x.getMessage()); } exitCond = 1; } } } if (!done){ try { if (action == ACTION_GUESS){ if(printErrors){ System.err.println(labels.getString("cantguess")); } exitCond = 1; } else if (action == ACTION_ENCODE){ encode( new BufferedInputStream(System.in), new BufferedOutputStream(System.out), lineBreaks ); } else { decode( new BufferedInputStream(System.in), new BufferedOutputStream(System.out), !forceDecode ); if (decodeEndLine) System.out.println(); } } catch (Base64DecodingException x){ if(printErrors){ System.err.println(x.getMessage() + " " + labels.getString("unexpectedcharforce")); } exitCond = 1; } catch (IOException x){ if(printErrors){ System.err.println(x.getMessage()); } exitCond = 1; } } System.exit(exitCond); } /** * Encode a String in Base64. * The String is converted to and from bytes according to the platform's * default character encoding. * No line breaks or other white space are inserted into the encoded data. * * @param string The data to encode. * @return An encoded String. * * @since ostermillerutils 1.00.00 */ public static String encode(String string){ return new String(encode(string.getBytes())); } /** * Encode a String in Base64. * No line breaks or other white space are inserted into the encoded data. * * @param string The data to encode. * @param enc Character encoding to use when converting to and from bytes. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @return An encoded String. * * @since ostermillerutils 1.00.00 */ public static String encode(String string, String enc) throws UnsupportedEncodingException { return new String(encode(string.getBytes(enc)), enc); } /** * Encode bytes in Base64. * No line breaks or other white space are inserted into the encoded data. * * @param bytes The data to encode. * @return Encoded bytes. * * @since ostermillerutils 1.00.00 */ public static byte[] encode(byte[] bytes){ ByteArrayInputStream in = new ByteArrayInputStream(bytes); // calculate the length of the resulting output. // in general it will be 4/3 the size of the input // but the input length must be divisible by three. // If it isn't the next largest size that is divisible // by three is used. int mod; int length = bytes.length; if ((mod = length % 3) != 0){ length += 3 - mod; } length = length * 4 / 3; ByteArrayOutputStream out = new ByteArrayOutputStream(length); try { encode(in, out, false); } catch (IOException x){ // This can't happen. // The input and output streams were constructed // on memory structures that don't actually use IO. throw new RuntimeException(x); } return out.toByteArray(); } /** * Encode this file in Base64. * Line breaks will be inserted every 76 characters. * * @param fIn File to be encoded (will be overwritten). * * @since ostermillerutils 1.00.00 */ public static void encode(File fIn) throws IOException { encode(fIn, fIn, true); } /** * Encode this file in Base64. * * @param fIn File to be encoded (will be overwritten). * @param lineBreaks Whether to insert line breaks every 76 characters in the output. * @throws IOException if an input or output error occurs. * * @since ostermillerutils 1.00.00 */ public static void encode(File fIn, boolean lineBreaks) throws IOException { encode(fIn, fIn, lineBreaks); } /** * Encode this file in Base64. * Line breaks will be inserted every 76 characters. * * @param fIn File to be encoded. * @param fOut File to which the results should be written (may be the same as fIn). * @throws IOException if an input or output error occurs. * * @since ostermillerutils 1.00.00 */ public static void encode(File fIn, File fOut) throws IOException { encode(fIn, fOut, true); } /** * Encode this file in Base64. * * @param fIn File to be encoded. * @param fOut File to which the results should be written (may be the same as fIn). * @param lineBreaks Whether to insert line breaks every 76 characters in the output. * @throws IOException if an input or output error occurs. * * @since ostermillerutils 1.00.00 */ public static void encode(File fIn, File fOut, boolean lineBreaks) throws IOException { File temp = null; InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(fIn)); temp = File.createTempFile("Base64", null, null); out = new BufferedOutputStream(new FileOutputStream(temp)); encode(in, out, lineBreaks); in.close(); in = null; out.flush(); out.close(); out = null; FileHelper.move(temp, fOut, true); } finally { if (in != null){ in.close(); in = null; } if (out != null){ out.flush(); out.close(); out = null; } } } /** * Encode data from the InputStream to the OutputStream in Base64. * Line breaks are inserted every 76 characters in the output. * * @param in Stream from which to read data that needs to be encoded. * @param out Stream to which to write encoded data. * @throws IOException if there is a problem reading or writing. * * @since ostermillerutils 1.00.00 */ public static void encode(InputStream in, OutputStream out) throws IOException { encode(in, out, true); } /** * Encode data from the InputStream to the OutputStream in Base64. * * @param in Stream from which to read data that needs to be encoded. * @param out Stream to which to write encoded data. * @param lineBreaks Whether to insert line breaks every 76 characters in the output. * @throws IOException if there is a problem reading or writing. * * @since ostermillerutils 1.00.00 */ public static void encode(InputStream in, OutputStream out, boolean lineBreaks) throws IOException { // Base64 encoding converts three bytes of input to // four bytes of output int[] inBuffer = new int[3]; int lineCount = 0; boolean done = false; while (!done && (inBuffer[0] = in.read()) != END_OF_INPUT){ // Fill the buffer inBuffer[1] = in.read(); inBuffer[2] = in.read(); // Calculate the out Buffer // The first byte of our in buffer will always be valid // but we must check to make sure the other two bytes // are not END_OF_INPUT before using them. // The basic idea is that the three bytes get split into // four bytes along these lines: // [AAAAAABB] [BBBBCCCC] [CCDDDDDD] // [xxAAAAAA] [xxBBBBBB] [xxCCCCCC] [xxDDDDDD]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -