Java Byte Array Create toBytesOctalEscaped(final byte[] s, final int off, final int len)

Here you can find the source of toBytesOctalEscaped(final byte[] s, final int off, final int len)

Description

to Bytes Octal Escaped

License

Open Source License

Declaration

private static byte[] toBytesOctalEscaped(final byte[] s, final int off, final int len) 

Method Source Code

//package com.java2s;
/*/*w  ww  . j a v a  2  s. c  o m*/
 * The MIT License
 *
 * Copyright 2015 Karol Bucek
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    private static final int MAX_3_BUFF_SIZE = 2 * 1024 * 1024;

    private static byte[] toBytesOctalEscaped(final byte[] s, final int off, final int len) {
        final byte[] out;
        final int end = off + len;
        int correctSize = len;
        if (len > MAX_3_BUFF_SIZE) {
            // count backslash escapes, they will be either
            // backslashes or an octal escape \\ or \003
            //
            for (int i = off; i < end; ++i) {
                if (s[i] == '\\') {
                    byte next = s[++i];
                    if (next == '\\') {
                        --correctSize;
                    } else {
                        correctSize -= 3;
                    }
                }
            }
            out = new byte[correctSize];
        } else {
            out = new byte[len];
        }

        int pos = 0;
        for (int i = off; i < end; i++) {
            final byte b = s[i];
            if (b == (byte) '\\') {
                final byte b1 = s[++i];
                if (b1 == (byte) '\\') { // escaped \
                    out[pos++] = (byte) '\\';
                } else {
                    int thebyte = (b1 - 48) * 64 + (s[++i] - 48) * 8 + (s[++i] - 48);
                    if (thebyte > 127)
                        thebyte -= 256;
                    out[pos++] = (byte) thebyte;
                }
            } else {
                out[pos++] = b;
            }
        }

        if (pos == correctSize)
            return out;

        final byte[] out2 = new byte[pos];
        System.arraycopy(out, 0, out2, 0, pos);
        return out2;
    }
}

Related

  1. toBytesFromString(String s)
  2. toBytesFromUnicode(String s)
  3. toBytesHexEscaped(final byte[] s, final int off, final int len)
  4. toBytesInt32BE(int w)
  5. toBytesLittleEndian(long value, int cnt)
  6. toBytesShortBE(short w)
  7. toBytesString(byte[] bytes)
  8. toByteString(byte[] bytes, String seperator)
  9. toByteString(byte[] content, int len)