comparison src/org/tmatesoft/hg/util/RegularFileInfo.java @ 287:ed6b74a58c66

Use FileInfo abstraction with necessary subset of File functionality instead of File to facilitate other effective file system iterators
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 07 Sep 2011 09:33:27 +0200
parents
children 981f9f50bb6c
comparison
equal deleted inserted replaced
286:954763c82cc3 287:ed6b74a58c66
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.util;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.ReadableByteChannel;
25
26 /**
27 *
28 * @author Artem Tikhomirov
29 * @author TMate Software Ltd.
30 */
31 public class RegularFileInfo implements FileInfo {
32 private File file;
33
34 public RegularFileInfo() {
35 }
36
37 public void init(File f) {
38 file = f;
39 }
40
41 public boolean exists() {
42 return file.canRead() && file.isFile();
43 }
44
45 public int lastModified() {
46 return (int) (file.lastModified() / 1000);
47 }
48
49 public long length() {
50 return file.length();
51 }
52
53 public ReadableByteChannel newInputChannel() {
54 try {
55 return new FileInputStream(file).getChannel();
56 } catch (FileNotFoundException ex) {
57 ex.printStackTrace(); // FIXME log debug.
58 // shall not happen, provided this class is used correctly
59 return new ReadableByteChannel() {
60
61 public boolean isOpen() {
62 return true;
63 }
64
65 public void close() throws IOException {
66 }
67
68 public int read(ByteBuffer dst) throws IOException {
69 // EOF right away
70 return -1;
71 }
72 };
73 }
74 }
75
76 }