Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *******************************************************************************    
 *   U2F BLE Tester
 *   (c) 2016 Ledger
 *   
 *  Licensed 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.
 ********************************************************************************/

import java.io.ByteArrayOutputStream;
import java.util.Vector;

public class Main {
    public static Vector<byte[]> split(int command, byte[] dataToTransport, int chunksize) {
        Vector<byte[]> result = new Vector<byte[]>();
        if (chunksize < 8) {
            throw new RuntimeException("Invalid chunk size");
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int remaining_length = dataToTransport.length;
        int offset = 0;
        int seq = 0;
        boolean firstPacket = true;

        while (remaining_length > 0) {
            int l = 0;
            if (!firstPacket) {
                baos.write(seq);
                l = Math.min(chunksize - 1, remaining_length);
            } else {
                baos.write(command);
                // first packet has the total transport length
                baos.write(remaining_length >> 8);
                baos.write(remaining_length);
                l = Math.min(chunksize - 3, remaining_length);
            }
            baos.write(dataToTransport, offset, l);
            remaining_length -= l;
            offset += l;
            result.add(baos.toByteArray());
            baos.reset();
            if (!firstPacket) {
                seq++;
            }
            firstPacket = false;
        }

        return result;
    }
}