Java Utililty Methods File Write via ByteBuffer

List of utility methods to do File Write via ByteBuffer

Description

The list of methods to do File Write via ByteBuffer are organized into topic(s).

Method

booleanwriteDataLengthsToHeader(FileOutputStream fpo)
write Data Lengths To Header
long len;
ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
FileChannel fc = fpo.getChannel();
try {
    len = fc.size();
    fc.position(4);
    bb.putInt((int) (len - 8));
    bb.flip();
...
voidwriteDelimitedToOutputStream(byte[] bytes, OutputStream outputStream)
Write length delimited data to the output stream
outputStream.write(ByteBuffer.allocate(4).putInt(bytes.length).array());
outputStream.write(bytes);
voidwriteDouble(BufferedWriter bw, double[] buf)
Write a given double array to the ASCII stream
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length - 1; ++i)
    sb.append(Double.toString(buf[i]) + " ");
sb.append(Double.toString(buf[buf.length - 1]) + "\n");
bw.append(sb.toString());
byte[]writeEmpty()
write Empty
return ByteBuffer.allocate(4).putInt(EMPTY_CLIPBOARD).array();
voidwriteFC(String fname, float[] res)
write FC
new File(fname).createNewFile();
ByteBuffer bbuffer = ByteBuffer.allocate(4 * res.length);
FloatBuffer buffer = bbuffer.asFloatBuffer();
for (int i = 0; i < res.length; i++)
    buffer.put(res[i]);
buffer.flip();
FileChannel fc = new RandomAccessFile(fname, "rw").getChannel();
fc.write(bbuffer);
...
voidwriteFile(File file, byte[] bytes)
write File
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel channel = raf.getChannel();
channel.truncate(0);
channel.write(ByteBuffer.wrap(bytes));
raf.close();
voidwriteFile(File file, byte[] data)
write File
FileOutputStream output = null;
FileChannel fc = null;
try {
    output = new FileOutputStream(file);
    fc = output.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(MAX_BUFFER_SIZE);
    int offset = 0;
    while (offset < data.length) {
...
voidwriteFile(String source, File outputFile)
write a string to a file; the file will be overwritten if it already exists
writeFile(source, outputFile, true);
voidwriteFileAsByteArray(File file, byte[] par2Data)
write File As Byte Array
ByteBuffer bb;
FileChannel fc = null;
try {
    fc = new FileOutputStream(file).getChannel();
    bb = ByteBuffer.wrap(par2Data);
    fc.write(bb);
} finally {
    if (fc != null)
...
voidwriteFileAsStringArray(File file, String[] par2DataArray)
write File As String Array
StringBuilder sb = new StringBuilder();
for (String t : par2DataArray) {
    sb.append(t);
writeFileAsString(file, sb.toString());