All Downloads are FREE. Search and download functionalities are using the official Maven repository.

t.21.120.2.source-code.FileUtil Maven / Gradle / Ivy

There is a newer version: 21.141.0
Show newest version
/////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2023 Garmin International, Inc.
// Licensed under the Flexible and Interoperable Data Transfer (FIT) Protocol License; you
// may not use this file except in compliance with the Flexible and Interoperable Data
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING****  This file is auto-generated!  Do NOT edit this file.
// Profile Version = 21.120Release
// Tag = production/release/21.120.00-0-g2d77811
/////////////////////////////////////////////////////////////////////////////////////////////


package com.garmin.fit;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import com.garmin.fit.Decode.RETURN;

public class FileUtil {

    static public Collection split(InputStream input) {
        Collection files = new ArrayList();

        try {
            byte[] data = new byte[input.available()];
            int pos = 0;
            byte fileHdrSize;
            int fileDataSize;
            int fileSize;

            input.read(data);

            while (pos < data.length) {
                fileHdrSize = data[pos];
                fileDataSize = data[pos + 4] & 0xFF;
                fileDataSize |= (data[pos + 5] & 0xFF) << 8;
                fileDataSize |= (data[pos + 6] & 0xFF) << 16;
                fileDataSize |= (data[pos + 7] & 0xFF) << 24;

                if (data[pos + 8] != '.') {
                    break;
                }

                if (data[pos + 9] != 'F') {
                    break;
                }

                if (data[pos + 10] != 'I') {
                    break;
                }

                if (data[pos + 11] != 'T'){
                    break;
                }

                fileSize = fileHdrSize + fileDataSize + 2;

                if ((pos + fileSize) > data.length) {
                    break;
                }

                files.add(Arrays.copyOfRange(data, pos, pos + fileSize));
                pos += fileSize;
            }

            if (pos < data.length) {
                files.add(Arrays.copyOfRange(data, pos, data.length));
            }
        } catch (IOException e) {
        }

        return files;
    }

    static public Collection prepend(Collection files, File fromFileType,
                                             File toFileType) {
        Collection fromFiles = new ArrayList();
        Collection toFiles = new ArrayList();
        Collection returnFiles = new ArrayList();

        // Read each file to get list of files to prepend.
        for (byte[] file : files) {
            Decode decode = new Decode();
            Decode.RETURN decodeReturn = RETURN.CONTINUE;
            boolean decodeComplete = false;
            int datum;
            ByteArrayInputStream fileStream = new ByteArrayInputStream(file);

            while (!decodeComplete) {
                datum = fileStream.read();

                if (datum < 0) {
                    System.out.println("end of stream");
                    break; // Unexpected end of stream.
                }

                decodeReturn = decode.read((byte) datum);

                switch (decodeReturn) {
                case MESG:
                    Mesg mesg = decode.getMesg();

                    if (mesg.name.equals("file_id")) {
                        FileIdMesg fileIdMesg = new FileIdMesg(mesg);

                        if (fileIdMesg.getType().equals(fromFileType)) {
                            fromFiles.add(file);
                        }

                        if (fileIdMesg.getType().equals(toFileType)) {
                            toFiles.add(file);
                        }

                        decodeComplete = true;
                    }
                    break;

                case END_OF_FILE:
                    decodeComplete = true;
                    break;

                default:
                    break;
                }
            }
        }

        // Create list of prepended files.
        for (byte[] file : files) {
            if (toFiles.contains(file)) {
                byte[] newFile;
                int newFileLength = file.length;
                int newFilePos = 0;

                for (byte[] fromFile : fromFiles) {
                    newFileLength += fromFile.length;
                }

                newFile = new byte[newFileLength];

                for (byte[] fromFile : fromFiles) {
                    System.arraycopy(fromFile, 0, newFile, newFilePos, fromFile.length);
                    newFilePos += fromFile.length;
                }

                System.arraycopy(file, 0, newFile, newFilePos, file.length);

                returnFiles.add(newFile);
            } else {
                returnFiles.add(file); // No files to prepend so pass straight through.
            }
        }

        return returnFiles;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy