集册 Java实例教程 确定传输编码看起来是否是有效的ascii文本,因此可以作为7bit代码进行传输。

确定传输编码看起来是否是有效的ascii文本,因此可以作为7bit代码进行传输。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

854
确定传输编码看起来是否可能是有效的ascii文本,从而可以作为7位代码进行传输。
/** 来 自 n o w j a v a . c o m**/

/*

 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file

 * distributed with this work for additional information

 * regarding copyright ownership.  The ASF licenses this file

 * to you under the Apache License, Version 2.0 (the

 * "License"); you may not use this file except in compliance

 * with the License.  You may obtain a copy of the License at

 *

 *  http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing,

 * software distributed under the License is distributed on an

 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

 * KIND, either express or implied.  See the License for the

 * specific language governing permissions and limitations

 * under the License.

 */

//package com.nowjava;

import java.io.BufferedInputStream;

import java.io.InputStream;

import java.io.IOException;


public class Main {

    /**

     * Determine if the transfer encoding looks like it might be

     * valid ascii text, and thus transferable as 7bit code.  In

     * order for this to be true, all characters must be valid

     * 7-bit ASCII code AND all line breaks must be properly formed

     * (JUST '\r\n' sequences).  7-bit transfers also

     * typically have a line limit of 1000 bytes (998 + the CRLF), so any

     * stretch of Characters longer than that will also force Base64 encoding.

     *

     * @param content     An input stream for the content we're examining.

     *

     * @exception IOException

     */

    public static String getBinaryTransferEncoding(InputStream content)

            throws IOException {


        // for efficiency, we'll read in blocks.

        BufferedInputStream in = new BufferedInputStream(content, 4096);


        int previousChar = 0;

        int span = 0; // span of characters without a line break.


        while (true) {

            int ch = in.read();

            // if we hit an EOF here, we've only found valid text so far, so we can transfer this as

            // 7-bit ascii.
            /* 
            *来 自
             nowjava - 时  代  Java
            */

            if (ch == -1) {

                return "7bit";

            }


            // we found a newline, this is only valid if the previous char was the '\r'

            if (ch == '\n') {

                // malformed linebreak?  force this to base64 encoding.

                if (previousChar != '\r') {

                    return "base64";

                }

                // hit a line end, reset our line length counter

                span = 0;

            } else {

                span++;

                // the text has long lines, we can't transfer this as unencoded text.

                if (span > 998) {

                    return "base64";

                }


                // non-ascii character, we have to transfer this in binary.

                if (!isAscii(ch)) {

                    return "base64";

                }

            }

            previousChar = ch;

        }

    }


    /**

     * Test to see if this string contains only US-ASCII (i.e., 7-bit

     * ASCII) Characters.

     *

     * @param s      The test string.

     *

     * @return true if this is a valid 7-bit ASCII encoding, false if it

     *         contains any non-US ASCII characters.

     */

    static public boolean isAscii(String s) {

        for (int i = 0; i < s.length(); i++) {

            if (!isAscii(s.charAt(i))) {

                return false;

            }

        }

        return true;

    }


    
展开阅读全文