tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/file/FileCompareOutputStream.java
changeset 56 aa2539c91954
parent 54 a151135b0cf9
child 60 e54443a6878c
child 62 1c2bb2fc7c87
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 * Output stream which compares the data to existing file before writing
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.file;
       
    20 
       
    21 import java.io.ByteArrayOutputStream;
       
    22 import java.io.File;
       
    23 import java.io.FileInputStream;
       
    24 import java.io.IOException;
       
    25 import java.io.OutputStream;
       
    26 
       
    27 /**
       
    28  * Output stream which writes to a byte array. When writing is complete, the
       
    29  * array contents are compared to existing file and the file is written only if
       
    30  * there are changes
       
    31  * 
       
    32  */
       
    33 public class FileCompareOutputStream extends ByteArrayOutputStream {
       
    34 
       
    35 	/**
       
    36 	 * The header file
       
    37 	 */
       
    38 	private File file;
       
    39 
       
    40 	/**
       
    41 	 * Creates a new stream
       
    42 	 * 
       
    43 	 * @param file
       
    44 	 *            the header file
       
    45 	 */
       
    46 	public FileCompareOutputStream(File file) {
       
    47 		this.file = file;
       
    48 	}
       
    49 
       
    50 	/**
       
    51 	 * Writes the buffer to file if it has changed
       
    52 	 * 
       
    53 	 * @return true if header was written, false if it matched the existing
       
    54 	 *         header
       
    55 	 * @throws IOException
       
    56 	 *             if writing fails
       
    57 	 */
       
    58 	public boolean writeFile() throws IOException {
       
    59 		// Compares the file length to number of bytes in array
       
    60 		boolean writeHeader = true;
       
    61 		if (file.length() == count) {
       
    62 			FileInputStream fis = new FileInputStream(file);
       
    63 			byte[] temp = new byte[count];
       
    64 			fis.read(temp);
       
    65 			fis.close();
       
    66 			writeHeader = false;
       
    67 			// Compares the file content
       
    68 			for (int i = 0; i < count; i++) {
       
    69 				if (temp[i] != buf[i]) {
       
    70 					writeHeader = true;
       
    71 					i = count;
       
    72 				}
       
    73 			}
       
    74 		}
       
    75 		if (writeHeader) {
       
    76 			OutputStream fos = FileUtils.createOutputStream(file);
       
    77 			writeTo(fos);
       
    78 			fos.close();
       
    79 		}
       
    80 		return writeHeader;
       
    81 	}
       
    82 }