libepg.util.bytearray.ByteArraySplitter.java Source code

Java tutorial

Introduction

Here is the source code for libepg.util.bytearray.ByteArraySplitter.java

Source

/*
 * Copyright (C) 2016 normal
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package libepg.util.bytearray;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import epgtools.loggerfactory.LoggerFactory;

/**
 *
 * @author normal
 */
public final class ByteArraySplitter {

    /**
     * false?????????????
     */
    public static final boolean CLASS_LOG_OUTPUT_MODE = true;

    private static final Log LOG;

    static {
        final Class<?> myClass = MethodHandles.lookup().lookupClass();
        LOG = new LoggerFactory(myClass, ByteArraySplitter.CLASS_LOG_OUTPUT_MODE).getLOG();
    }

    private ByteArraySplitter() {
    }

    /**
     * ????????????????????0??<br>
     * 1.??????????????????<br>
     * 2.??????????????????????????<br>
     *
     *
     * @author normal
     * @param src ?
     * @param lengthFieldPosition
     * ??(??????????????)<br>
     * @param lengthFieldLength ?????
     * @param preProcessor ???8?????????????
     * @return ???
     *
     * ????<br>
     * X=?????<br>
     * Y=?<br>
     * Z=????<br>
     * ???=XXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZXXXXYYZZZZZZZ<br>
     * ???1=XXXXYYZZZZZZ<br>
     * ???2=XXXXYYZZZZZZ<br>
     * ???3=XXXXYYZZZZZZ<br>
     * ???4=XXXXYYZZZZZZ<br>
     * ???5=XXXXYYZZZZZZ<br>
     * ???6=XXXXYYZZZZZZ<br>
     * ???7=XXXXYYZZZZZZ<br>
     * ???8=XXXXYYZZZZZZ<br>
     * ???9=XXXXYYZZZZZZZ<br>
     */
    public static synchronized List<byte[]> splitByLengthField(byte[] src, int lengthFieldPosition,
            int lengthFieldLength, UnaryOperator<Integer> preProcessor) {

        UnaryOperator<Integer> preProcessor_t;
        if (preProcessor == null) {
            throw new NullPointerException("??????????");
        } else {
            preProcessor_t = preProcessor;
        }

        if (LOG.isTraceEnabled()) {
            LOG.trace(" = " + Hex.encodeHexString(src));
            LOG.trace("?? = " + lengthFieldPosition);
            LOG.trace("??? = " + lengthFieldLength);
        }

        List<byte[]> dest = new ArrayList<>();
        try (ByteArrayInputStream bis = new ByteArrayInputStream(src)) {
            while (bis.available() > 0) {
                //??????????????????
                bis.mark(0);

                //???????????????????????
                bis.skip(lengthFieldPosition - 1);
                //??
                byte[] lengthFieldValue_Byte = new byte[lengthFieldLength];
                if (bis.read(lengthFieldValue_Byte) == -1) {
                    break;
                }
                int lengthFieldValue = ByteConverter.bytesToInt(lengthFieldValue_Byte);//?
                int lengthFieldValue_Processed = preProcessor_t.apply(lengthFieldValue);

                if (LOG.isTraceEnabled()) {
                    LOG.trace("?(byte) = " + Hex.encodeHexString(lengthFieldValue_Byte));
                    LOG.trace("?(?) = " + lengthFieldValue);
                    LOG.trace("?() = " + lengthFieldValue_Processed);
                }
                //??
                bis.reset();

                //???????(??????-1+???+??)
                int partLength = lengthFieldPosition - 1 + lengthFieldLength + lengthFieldValue_Processed;
                if (LOG.isTraceEnabled()) {
                    LOG.trace("?????= " + partLength);
                }

                //???
                byte[] temp = new byte[partLength];

                //?
                if (bis.read(temp) == -1) {
                    break;
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace("?= " + Hex.encodeHexString(temp));
                }
                dest.add(temp);
            }
        } catch (IOException ex) {
            LOG.info("???????????");
        } finally {
            return dest;
        }
    }

    /**
     * ?????????0??
     *
     * @param src ?
     * @param size ????
     * @return ????
     */
    public static synchronized List<byte[]> split(byte[] src, int size) {
        List<byte[]> dest = new ArrayList<>();
        try (ByteArrayInputStream bis = new ByteArrayInputStream(src)) {
            while (bis.available() > 0) {
                //??
                bis.mark(0);
                //???
                byte[] temp = new byte[size];
                //?
                if (bis.read(temp) == -1) {
                    break;
                }
                dest.add(temp);
            }
        } catch (IOException ex) {
            LOG.info("???????????");
        } finally {
            return dest;
        }
    }

    /**
     * ?????????n?????????
     *
     * @see ByteArraySplitter.splitByLengthField
     */
    public static synchronized List<byte[]> splitByLengthField(byte[] src, int lengthFieldPosition,
            int lengthFieldLength) {
        return splitByLengthField(src, lengthFieldPosition, lengthFieldLength, (x) -> x);
    }
}