pieShareAppITs.helper.ITFileUtils.java Source code

Java tutorial

Introduction

Here is the source code for pieShareAppITs.helper.ITFileUtils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pieShareAppITs.helper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Svetoslav
 */
public class ITFileUtils {

    public static File createFile(File file, long size) throws FileNotFoundException, IOException {
        int defaultSize = 1024;
        if (size < defaultSize) {
            defaultSize = (int) size;
        }

        long max = size / defaultSize;
        long delta = size - (max * defaultSize);
        Random random = new Random();
        byte[] data = new byte[defaultSize];

        for (int i = 0; i < max; i++) {
            random.nextBytes(data);
            FileUtils.writeByteArrayToFile(file, data, true);
        }

        data = new byte[(int) delta];
        random.nextBytes(data);
        FileUtils.writeByteArrayToFile(file, data, true);

        return file;
    }
}