1 /*
2  * Copyright (c) 2017 Balabit
3  * Copyright (c) 2017 Zoltan Pallagi <zoltan.pallagi@balabit.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * As an additional exemption you are allowed to compile & link against the
19  * OpenSSL libraries as published by the OpenSSL project. See the file
20  * COPYING for details.
21  *
22  */
23 
24 package org.syslog_ng.hdfs;
25 
26 import org.apache.hadoop.fs.FSDataOutputStream;
27 import org.apache.hadoop.fs.Path;
28 
29 import java.io.IOException;
30 
31 public class HdfsFile {
32     private FSDataOutputStream fsDataOutputStream;
33     private Path path;
34     private long lastWrite;
35 
HdfsFile(Path filepath, FSDataOutputStream outputstream)36     public HdfsFile(Path filepath, FSDataOutputStream outputstream) {
37         this.path = filepath;
38         this.fsDataOutputStream = outputstream;
39         this.lastWrite = 0;
40     }
41 
getPath()42     public Path getPath() {
43         return path;
44     }
45 
isOpen()46     public boolean isOpen() {
47         return fsDataOutputStream != null;
48     }
49 
flush()50     public void flush() throws IOException {
51          if (!isOpen())
52             return;
53 
54          fsDataOutputStream.hflush();
55          fsDataOutputStream.hsync();
56     }
57 
write(byte[] message)58     public void write(byte[] message) throws IOException {
59          if (!isOpen())
60             throw new IOException("File is not open: "+path);
61 
62          fsDataOutputStream.write(message);
63 
64          lastWrite = System.currentTimeMillis();
65     }
66 
timeSinceLastWrite()67     public long timeSinceLastWrite() {
68        if (!isOpen())
69           return 0;
70 
71        return System.currentTimeMillis() - lastWrite;
72     }
73 
close()74     public void close() throws IOException {
75          if (!isOpen())
76             return;
77 
78           fsDataOutputStream.close();
79           fsDataOutputStream = null;
80           lastWrite = 0;
81     }
82 }
83