587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 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 the License "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 |
*/
|
|
17 |
package com.nokia.helium.core.plexus;
|
|
18 |
|
628
|
19 |
import java.io.BufferedWriter;
|
587
|
20 |
import java.io.File;
|
|
21 |
import java.io.FileNotFoundException;
|
|
22 |
import java.io.FileOutputStream;
|
|
23 |
import java.io.IOException;
|
|
24 |
import java.io.OutputStreamWriter;
|
|
25 |
|
|
26 |
import org.apache.log4j.Logger;
|
|
27 |
import org.codehaus.plexus.util.cli.StreamConsumer;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Record a stream into a file.
|
|
31 |
*
|
|
32 |
*/
|
|
33 |
public class FileStreamConsumer implements StreamConsumer {
|
|
34 |
private Logger log = Logger.getLogger(getClass());
|
|
35 |
private BufferedWriter writer;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Create a FileStreamConsumer which will record content to
|
|
39 |
* the output file.
|
|
40 |
* @param output the file to write the output to.
|
|
41 |
* @throws FileNotFoundException if an error occur while opening the file.
|
|
42 |
*/
|
|
43 |
public FileStreamConsumer(File output) throws FileNotFoundException {
|
|
44 |
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
|
|
45 |
}
|
|
46 |
|
|
47 |
/**
|
|
48 |
* {@inheritDoc}
|
|
49 |
*/
|
|
50 |
@Override
|
|
51 |
public synchronized void consumeLine(String line) {
|
|
52 |
try {
|
|
53 |
writer.write(line);
|
|
54 |
writer.newLine();
|
|
55 |
} catch (IOException e) {
|
|
56 |
log.error("Error while writing to file: " + e.getMessage(), e);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Closing the file.
|
|
62 |
*/
|
|
63 |
public void close() {
|
|
64 |
try {
|
|
65 |
writer.flush();
|
|
66 |
writer.close();
|
|
67 |
} catch (IOException e) {
|
|
68 |
log.error("Error while writing to file: " + e.getMessage(), e);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Helper function to return the writer instance for sub classes
|
|
74 |
* to write if any additional information.
|
|
75 |
* @return writer of the stream consumer.
|
|
76 |
*/
|
|
77 |
public BufferedWriter getWriter() {
|
|
78 |
return writer;
|
|
79 |
}
|
|
80 |
}
|