
// SparaOchLasaByteBin.java

import java.io.*;
import java.util.*;

/******************************************************************************
/ SparaOchLasaByteBin.java skapar en bytevektor samt sparar dess längd och
/ innehåll i en binärfil. Därefter läses vektorn in och skrivs ut.
/ Både obuffrad och buffrad IO används.
/
/   FileOutputStream
/   FileInputStream
/   BufferedOutputStream
/   BufferedInputStream
/
/*****************************************************************************/

public class SparaOchLasaByteBin
{

    public static void main(String[] args) throws java.io.IOException
    {
		SparaOchLasaByteObuffrat ();
		SparaOchLasaByteBuffrat ();
    }

/******************************************************************************
/ SparaOchLasaByte() skapar en bytevektor samt sparar dess längd och innehåll i
/ en binärfil, obuffrat.
/
/ IN : -
/
/ UT : (void)
/
/*****************************************************************************/
    public static void SparaOchLasaByteObuffrat() throws java.io.IOException
    {
		System.out.println ("---- Obuffrad Byte-IO med bin-fil ----");
		FileOutputStream    fout = new FileOutputStream ("bytes.dat");
		
		byte[]    bv = {1, 2, 3, 4, 5, 6, 7, 9, 'A', 'x'};
		int    i = bv.length;
		
		fout.write (i);
		fout.write (bv);
		
		fout.close ();
		
		// ----
		
		FileInputStream    fin = new FileInputStream ("bytes.dat");
		
		int i2 = fin.read ();
		byte[]    bv2 = new byte[i2];
		
		fin.read (bv2);
		
		fin.close ();
		
		// ----
		
		System.out.println (i2);
		System.out.println (Arrays.toString(bv2));
		System.out.println ();
		
	}

/******************************************************************************
/ SparaOchLasaBuffrat() skapar en bytevektor samt sparar dess längd och inne-
/ håll i en binärfil, buffrat.
/
/ IN : -
/
/ UT : (void)
/
/*****************************************************************************/
    public static void SparaOchLasaByteBuffrat() throws java.io.IOException
    {
		System.out.println ();
		System.out.println ("---- Buffrad Byte-IO med bin-fil ----");
		
		FileOutputStream    fos = new FileOutputStream ("bytes_buf.dat");
		BufferedOutputStream    fout = new BufferedOutputStream (fos);
		
		byte[]    bv = {9, 3, 120, 65, 66, 67, 'q', 'w'};
		int    i = bv.length;
		
		fout.write (i);
		
		fout.flush ();
		
		fout.write (bv);
		
		fout.close ();
		
		// ----
		
		FileInputStream    fis = new FileInputStream ("bytes_buf.dat");
		BufferedInputStream    fin = new BufferedInputStream (fis);
		
		int i2 = fin.read ();
		byte[]    bv2 = new byte[i2];
		
		fin.read (bv2);
		
		fin.close ();
		
		// ----
		
		System.out.println (i2);
		System.out.println (Arrays.toString (bv2));
		System.out.println ();
		
	}
}