Java List Last Item flattenByteSegments(List byteSegments, int eachSegmentButLastLength, int lastSegmentLength)

Here you can find the source of flattenByteSegments(List byteSegments, int eachSegmentButLastLength, int lastSegmentLength)

Description

Turns a List of equally-sized byte segments (buffers) into a single buffer by concatenating them one by one.

License

Open Source License

Parameter

Parameter Description
byteSegments The buffers to "flatten"
eachSegmentButLastLength The length of each buffer but the last -- they are assumed all to be the same size
lastSegmentLength The length of the last buffer, since it is likely not the same length as the others

Declaration

public static byte[] flattenByteSegments(List byteSegments, int eachSegmentButLastLength,
        int lastSegmentLength) 

Method Source Code

//package com.java2s;
/*/*from   w  ww .  j  av  a  2s. c  o m*/
 *  Copyright (C) 1998, 2003 Gargoyle Software. All rights reserved.
 *
 *  This file is part of GSBase. For details on use and redistribution
 *  please refer to the license.html file included with these sources.
 */

import java.util.*;

public class Main {
    /***
     * Turns a List of equally-sized byte segments (buffers) 
     * into a single buffer
     * by concatenating them one by one.
     * 
     * @param byteSegments  The buffers to "flatten"
     * @param eachSegmentButLastLength  The length of each buffer
     * but the last -- they are assumed all to be the same size
     * @param lastSegmentLength  The length of the last buffer,
     * since it is likely not the same length as the others
     * @return
     */
    public static byte[] flattenByteSegments(List byteSegments, int eachSegmentButLastLength,
            int lastSegmentLength) {

        if (byteSegments.isEmpty())
            return new byte[0];
        else {
            int totalBytes = eachSegmentButLastLength * (byteSegments.size() - 1) + lastSegmentLength;

            byte[] flattenedBytes = new byte[totalBytes];

            int nextSegmentOffset = 0;

            for (int i = 0; i < byteSegments.size() - 1; i++) {
                System.arraycopy(byteSegments.get(i), 0, flattenedBytes, nextSegmentOffset,
                        eachSegmentButLastLength);

                nextSegmentOffset += eachSegmentButLastLength;
            }

            System.arraycopy(
                    truncateByteSegment((byte[]) byteSegments.get(byteSegments.size() - 1), lastSegmentLength), 0,
                    flattenedBytes, nextSegmentOffset, lastSegmentLength);

            return flattenedBytes;
        }
    }

    private static byte[] truncateByteSegment(byte[] byteSegment, int toSize) {

        byte[] newSegment = new byte[toSize];
        System.arraycopy(byteSegment, 0, newSegment, 0, toSize);
        return newSegment;
    }
}

Related

  1. addDelimiter(List lines, String delimiter, boolean isDelimitedLastLine)
  2. addLast(T e, List list)
  3. addLastElement(List list, T element)
  4. buildPathFromComponentsUpToIndex(final List pathComponents, final int lastIndex)
  5. getLast(final List list)
  6. getLast(final List list)
  7. getLast(List list)
  8. getLast(List aList)