Here you can find the source of flattenByteSegments(List byteSegments, int eachSegmentButLastLength, int lastSegmentLength)
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 |
public static byte[] flattenByteSegments(List byteSegments, int eachSegmentButLastLength, int lastSegmentLength)
//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; } }