Revision 42231
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/DefaultBackupGeneratorFactory.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.tools.backup; |
|
25 |
|
|
26 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
27 |
* |
|
28 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
29 |
* of the Valencian Government (CIT) |
|
30 |
* |
|
31 |
* This program is free software; you can redistribute it and/or |
|
32 |
* modify it under the terms of the GNU General Public License |
|
33 |
* as published by the Free Software Foundation; either version 2 |
|
34 |
* of the License, or (at your option) any later version. |
|
35 |
* |
|
36 |
* This program is distributed in the hope that it will be useful, |
|
37 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
38 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
39 |
* GNU General Public License for more details. |
|
40 |
* |
|
41 |
* You should have received a copy of the GNU General Public License |
|
42 |
* along with this program; if not, write to the Free Software |
|
43 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
44 |
* MA 02110-1301, USA. |
|
45 |
* |
|
46 |
*/ |
|
47 |
|
|
48 |
/** |
|
49 |
* <p>Factory that gets a {@link DefaultBackupGenerator DefaultBackupGenerator} as a particular version of |
|
50 |
* backup generator.</p> |
|
51 |
* |
|
52 |
* @author Jose Ignacio Yarza (jiyarza@opensistemas.com) |
|
53 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) |
|
54 |
*/ |
|
55 |
public class DefaultBackupGeneratorFactory extends BackupGeneratorFactory { |
|
56 |
/* |
|
57 |
* (non-Javadoc) |
|
58 |
* @see com.iver.utiles.backup.BackupGeneratorFactory#getBackupGenerator() |
|
59 |
*/ |
|
60 |
public BackupGenerator getBackupGenerator() { |
|
61 |
return new DefaultBackupGenerator(); |
|
62 |
} |
|
63 |
} |
|
0 | 64 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/BackupGenerator.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.tools.backup; |
|
25 |
|
|
26 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
27 |
* |
|
28 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
29 |
* of the Valencian Government (CIT) |
|
30 |
* |
|
31 |
* This program is free software; you can redistribute it and/or |
|
32 |
* modify it under the terms of the GNU General Public License |
|
33 |
* as published by the Free Software Foundation; either version 2 |
|
34 |
* of the License, or (at your option) any later version. |
|
35 |
* |
|
36 |
* This program is distributed in the hope that it will be useful, |
|
37 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
38 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
39 |
* GNU General Public License for more details. |
|
40 |
* |
|
41 |
* You should have received a copy of the GNU General Public License |
|
42 |
* along with this program; if not, write to the Free Software |
|
43 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
44 |
* MA 02110-1301, USA. |
|
45 |
* |
|
46 |
*/ |
|
47 |
|
|
48 |
import java.io.File; |
|
49 |
|
|
50 |
import org.gvsig.tools.backup.exceptions.BackupException; |
|
51 |
|
|
52 |
|
|
53 |
/** |
|
54 |
* <p>A tagging interface that all file backups must implement.</p> |
|
55 |
* |
|
56 |
* @author Jose Ignacio Yarza (jiyarza@opensistemas.com) |
|
57 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) |
|
58 |
*/ |
|
59 |
public interface BackupGenerator { |
|
60 |
/** |
|
61 |
* <p>Performs a backup of <code>source</code>.</p> |
|
62 |
* |
|
63 |
* @param source the source file |
|
64 |
*/ |
|
65 |
public void backup(File source) throws BackupException; |
|
66 |
} |
|
0 | 67 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/DefaultBackupGenerator.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.tools.backup; |
|
25 |
|
|
26 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
27 |
* |
|
28 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
29 |
* of the Valencian Government (CIT) |
|
30 |
* |
|
31 |
* This program is free software; you can redistribute it and/or |
|
32 |
* modify it under the terms of the GNU General Public License |
|
33 |
* as published by the Free Software Foundation; either version 2 |
|
34 |
* of the License, or (at your option) any later version. |
|
35 |
* |
|
36 |
* This program is distributed in the hope that it will be useful, |
|
37 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
38 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
39 |
* GNU General Public License for more details. |
|
40 |
* |
|
41 |
* You should have received a copy of the GNU General Public License |
|
42 |
* along with this program; if not, write to the Free Software |
|
43 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
44 |
* MA 02110-1301, USA. |
|
45 |
* |
|
46 |
*/ |
|
47 |
|
|
48 |
import java.io.File; |
|
49 |
import java.io.FileInputStream; |
|
50 |
import java.io.FileOutputStream; |
|
51 |
import java.nio.channels.FileChannel; |
|
52 |
|
|
53 |
import org.gvsig.tools.backup.exceptions.BackupException; |
|
54 |
|
|
55 |
|
|
56 |
/** |
|
57 |
* <p>Performs a backup of a file, into another file at the same path (directory), with the file extension |
|
58 |
* changed to <i>.bak</i>.</p> |
|
59 |
* |
|
60 |
* @author Jose Ignacio Yarza (jiyarza@opensistemas.com) |
|
61 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) |
|
62 |
*/ |
|
63 |
public class DefaultBackupGenerator implements BackupGenerator { |
|
64 |
/* |
|
65 |
* (non-Javadoc) |
|
66 |
* @see com.iver.utiles.backup.BackupGenerator#backup(java.io.File) |
|
67 |
*/ |
|
68 |
public void backup(File source) throws BackupException { |
|
69 |
try { |
|
70 |
int index = source.getAbsolutePath().lastIndexOf("."); |
|
71 |
|
|
72 |
if (index == -1) |
|
73 |
return; |
|
74 |
|
|
75 |
File dest = new File(source.getAbsolutePath().substring(0, index) + ".bak"); |
|
76 |
|
|
77 |
// Create channel on the source |
|
78 |
FileChannel srcChannel = new FileInputStream(source).getChannel(); |
|
79 |
|
|
80 |
// Create channel on the destination |
|
81 |
FileChannel dstChannel = new FileOutputStream(dest).getChannel(); |
|
82 |
|
|
83 |
// Copy file contents from source to destination |
|
84 |
dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); |
|
85 |
|
|
86 |
// Close the channels |
|
87 |
srcChannel.close(); |
|
88 |
dstChannel.close(); |
|
89 |
} catch (Exception ex) { |
|
90 |
throw new BackupException(ex.getMessage(), ex, source); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
0 | 94 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/exceptions/BackupException.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.tools.backup.exceptions; |
|
25 |
|
|
26 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
27 |
* |
|
28 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
29 |
* of the Valencian Government (CIT) |
|
30 |
* |
|
31 |
* This program is free software; you can redistribute it and/or |
|
32 |
* modify it under the terms of the GNU General Public License |
|
33 |
* as published by the Free Software Foundation; either version 2 |
|
34 |
* of the License, or (at your option) any later version. |
|
35 |
* |
|
36 |
* This program is distributed in the hope that it will be useful, |
|
37 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
38 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
39 |
* GNU General Public License for more details. |
|
40 |
* |
|
41 |
* You should have received a copy of the GNU General Public License |
|
42 |
* along with this program; if not, write to the Free Software |
|
43 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
44 |
* MA 02110-1301, USA. |
|
45 |
* |
|
46 |
*/ |
|
47 |
|
|
48 |
import java.io.File; |
|
49 |
|
|
50 |
/** |
|
51 |
* <p>Exception to report that a backup process has failed.</p> |
|
52 |
* |
|
53 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) |
|
54 |
*/ |
|
55 |
public class BackupException extends Exception { |
|
56 |
private static final long serialVersionUID = -2846421984617208883L; |
|
57 |
|
|
58 |
/** |
|
59 |
* <p>The source file to be backup.</p> |
|
60 |
*/ |
|
61 |
protected File source; |
|
62 |
|
|
63 |
/** |
|
64 |
* <p>Constructs a new backup exception with the specified detail message and cause.</p> |
|
65 |
* |
|
66 |
* @param message the detail message (which is saved for later retrieval by the <code>getMessage()</code> method). |
|
67 |
* @param cause the cause (which is saved for later retrieval by the <code>getCause()</code> method). (A <code>null</code> |
|
68 |
* value is permitted, and indicates that the cause is nonexistent or unknown.) |
|
69 |
* @param source the file from that was going to be done a backup |
|
70 |
* |
|
71 |
* @see Exception#Exception(String, Throwable) |
|
72 |
*/ |
|
73 |
public BackupException(String message, Throwable cause, File source) { |
|
74 |
super(message, cause); |
|
75 |
this.source = source; |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* <p>Gets the source file to be backup.</p> |
|
80 |
* |
|
81 |
* @return the source file |
|
82 |
*/ |
|
83 |
public File getSource() { |
|
84 |
return source; |
|
85 |
} |
|
86 |
} |
|
0 | 87 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/BackupGeneratorFactory.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.tools.backup; |
|
25 |
|
|
26 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
27 |
* |
|
28 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
29 |
* of the Valencian Government (CIT) |
|
30 |
* |
|
31 |
* This program is free software; you can redistribute it and/or |
|
32 |
* modify it under the terms of the GNU General Public License |
|
33 |
* as published by the Free Software Foundation; either version 2 |
|
34 |
* of the License, or (at your option) any later version. |
|
35 |
* |
|
36 |
* This program is distributed in the hope that it will be useful, |
|
37 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
38 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
39 |
* GNU General Public License for more details. |
|
40 |
* |
|
41 |
* You should have received a copy of the GNU General Public License |
|
42 |
* along with this program; if not, write to the Free Software |
|
43 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
44 |
* MA 02110-1301, USA. |
|
45 |
* |
|
46 |
*/ |
|
47 |
|
|
48 |
/** |
|
49 |
* <p>Generic factory that creates a {@link BackupGenerator BackupGenerator} that performs a particular |
|
50 |
* kind of backup.</p> |
|
51 |
* |
|
52 |
* @author Jose Ignacio Yarza (jiyarza@opensistemas.com) |
|
53 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) |
|
54 |
*/ |
|
55 |
public abstract class BackupGeneratorFactory { |
|
56 |
/** |
|
57 |
* <p>Gets an instance of a backup generator that performs a particular kind of backup.</p> |
|
58 |
* |
|
59 |
* @return an instance of a backup generator that performs a particular kind of backup |
|
60 |
*/ |
|
61 |
public abstract BackupGenerator getBackupGenerator(); |
|
62 |
} |
|
0 | 63 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/IverUtilesLibrary.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.tools; |
|
25 |
|
|
26 |
import org.gvsig.tools.library.AbstractLibrary; |
|
27 |
import org.gvsig.tools.library.LibraryException; |
|
28 |
import org.gvsig.utils.swing.jcomboServer.ServerData; |
|
29 |
|
|
30 |
public class IverUtilesLibrary extends AbstractLibrary { |
|
31 |
|
|
32 |
@Override |
|
33 |
public void doRegistration() { |
|
34 |
registerAsAPI(IverUtilesLibrary.class); |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
protected void doInitialize() throws LibraryException { |
|
39 |
// Nothing to do |
|
40 |
} |
|
41 |
|
|
42 |
@Override |
|
43 |
protected void doPostInitialize() throws LibraryException { |
|
44 |
ServerData.registerPersistence(); |
|
45 |
} |
|
46 |
} |
|
0 | 47 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/SymbolSet.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.utils; |
|
25 |
|
|
26 |
/** |
|
27 |
* Conjunto de s?mbolos |
|
28 |
* |
|
29 |
* @author Fernando Gonz?lez Cort?s |
|
30 |
*/ |
|
31 |
public interface SymbolSet { |
|
32 |
/** |
|
33 |
* Devuelve true si el caracter c est? en el conjunto |
|
34 |
* |
|
35 |
* @param c caracter |
|
36 |
* |
|
37 |
* @return true si est? en el conjunto y false en caso contrario |
|
38 |
*/ |
|
39 |
public boolean contains(char c); |
|
40 |
} |
|
0 | 41 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/bigfile/BigByteBuffer.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.utils.bigfile; |
|
25 |
|
|
26 |
import java.io.IOException; |
|
27 |
import java.nio.Buffer; |
|
28 |
import java.nio.ByteBuffer; |
|
29 |
import java.nio.ByteOrder; |
|
30 |
import java.nio.CharBuffer; |
|
31 |
import java.nio.DoubleBuffer; |
|
32 |
import java.nio.FloatBuffer; |
|
33 |
import java.nio.IntBuffer; |
|
34 |
import java.nio.LongBuffer; |
|
35 |
import java.nio.MappedByteBuffer; |
|
36 |
import java.nio.ShortBuffer; |
|
37 |
import java.nio.channels.FileChannel; |
|
38 |
|
|
39 |
/** |
|
40 |
* @author Fjp |
|
41 |
* |
|
42 |
* Clase para trabajar con ficheros grandes. Mapea solo un trozo |
|
43 |
* de fichero en memoria, y cuando intentas acceder fuera de esa |
|
44 |
* zona, se ocupa de leer autom?ticamente el trozo que le falta. |
|
45 |
*/ |
|
46 |
public class BigByteBuffer { |
|
47 |
|
|
48 |
private static long DEFAULT_SIZE = 50*1024*1024; // 50 Megas m?ximo |
|
49 |
|
|
50 |
MappedByteBuffer bb; |
|
51 |
FileChannel fc; |
|
52 |
long minAbs, maxAbs, posAbs; |
|
53 |
int minRel, maxRel, posRel; |
|
54 |
long sizeChunk, amountMem; |
|
55 |
long fileSize; |
|
56 |
FileChannel.MapMode mode; |
|
57 |
|
|
58 |
/** |
|
59 |
* Revisa la posici?n absoluta, y si hace falta, carga el buffer |
|
60 |
* con la parte de fichero que toca. |
|
61 |
* @throws IOException |
|
62 |
*/ |
|
63 |
private void prepareBuffer(long posActual, int numBytesToRead) |
|
64 |
{ |
|
65 |
long desiredPos = posActual + numBytesToRead; |
|
66 |
if ((desiredPos > maxAbs) || (posActual < minAbs)) |
|
67 |
{ |
|
68 |
// Quiero leer fuera: |
|
69 |
sizeChunk = Math.min(fileSize-posActual, amountMem); |
|
70 |
try { |
|
71 |
ByteOrder lastOrder = bb.order(); |
|
72 |
bb = fc.map(mode, posActual, sizeChunk); |
|
73 |
minAbs = posActual; |
|
74 |
maxAbs = sizeChunk + posActual; |
|
75 |
bb.order(lastOrder); |
|
76 |
// System.out.println("BigByteBuffer: min=" + minAbs |
|
77 |
// + " maxAbs=" + maxAbs + " posAbs = " + posAbs); |
|
78 |
|
|
79 |
} catch (IOException e) { |
|
80 |
// TODO Auto-generated catch block |
|
81 |
e.printStackTrace(); |
|
82 |
} |
|
83 |
|
|
84 |
} |
|
85 |
// Dejamos posAbs apuntando a donde va a quedar |
|
86 |
// "a priori", antes de leer de verdad, que se hace |
|
87 |
// al salir de esta funci?n. |
|
88 |
posAbs = desiredPos; |
|
89 |
|
|
90 |
} |
|
91 |
|
|
92 |
public BigByteBuffer(FileChannel fc, FileChannel.MapMode mode, long amountMem) throws IOException |
|
93 |
{ |
|
94 |
this.amountMem = amountMem; |
|
95 |
this.fc = fc; |
|
96 |
this.fileSize = fc.size(); |
|
97 |
this.mode = mode; |
|
98 |
sizeChunk = Math.min(fc.size(), amountMem); |
|
99 |
bb = fc.map(mode, 0L, sizeChunk); |
|
100 |
minAbs = 0; |
|
101 |
maxAbs = sizeChunk; |
|
102 |
} |
|
103 |
public BigByteBuffer(FileChannel fc, FileChannel.MapMode mode) throws IOException |
|
104 |
{ |
|
105 |
this.amountMem = DEFAULT_SIZE; |
|
106 |
this.fc = fc; |
|
107 |
this.fileSize = fc.size(); |
|
108 |
this.mode = mode; |
|
109 |
sizeChunk = Math.min(fc.size(), amountMem); |
|
110 |
bb = fc.map(mode, 0L, sizeChunk); |
|
111 |
minAbs = 0; |
|
112 |
maxAbs = sizeChunk; |
|
113 |
} |
|
114 |
|
|
115 |
|
|
116 |
public synchronized byte get() { |
|
117 |
prepareBuffer(posAbs,1); |
|
118 |
return bb.get(); |
|
119 |
} |
|
120 |
public synchronized ByteBuffer get(byte[] dst) |
|
121 |
{ |
|
122 |
prepareBuffer(posAbs, dst.length); |
|
123 |
return bb.get(dst); |
|
124 |
} |
|
125 |
|
|
126 |
public synchronized char getChar() { |
|
127 |
prepareBuffer(posAbs,2); |
|
128 |
return bb.getChar(); |
|
129 |
} |
|
130 |
|
|
131 |
public synchronized double getDouble() { |
|
132 |
prepareBuffer(posAbs,8); |
|
133 |
return bb.getDouble(); |
|
134 |
} |
|
135 |
|
|
136 |
public synchronized float getFloat() { |
|
137 |
prepareBuffer(posAbs,4); |
|
138 |
return bb.getFloat(); |
|
139 |
} |
|
140 |
|
|
141 |
public synchronized int getInt() { |
|
142 |
prepareBuffer(posAbs,4); |
|
143 |
return bb.getInt(); |
|
144 |
} |
|
145 |
|
|
146 |
public synchronized long getLong() { |
|
147 |
prepareBuffer(posAbs,8); |
|
148 |
return bb.getLong(); |
|
149 |
} |
|
150 |
|
|
151 |
public synchronized short getShort() { |
|
152 |
prepareBuffer(posAbs,2); |
|
153 |
return bb.getShort(); |
|
154 |
} |
|
155 |
|
|
156 |
public boolean isDirect() { |
|
157 |
return bb.isDirect(); |
|
158 |
} |
|
159 |
|
|
160 |
public synchronized byte get(int index) { |
|
161 |
prepareBuffer(index,1); |
|
162 |
return bb.get(index - (int) minAbs); |
|
163 |
} |
|
164 |
|
|
165 |
public synchronized char getChar(int index) { |
|
166 |
prepareBuffer(index,2); |
|
167 |
return bb.getChar(index - (int) minAbs); |
|
168 |
} |
|
169 |
|
|
170 |
public synchronized double getDouble(int index) { |
|
171 |
prepareBuffer(index,8); |
|
172 |
return bb.getDouble(index - (int) minAbs); |
|
173 |
} |
|
174 |
|
|
175 |
public synchronized float getFloat(int index) { |
|
176 |
prepareBuffer(index,4); |
|
177 |
return bb.getFloat(index - (int) minAbs); |
|
178 |
} |
|
179 |
|
|
180 |
public synchronized int getInt(int index) { |
|
181 |
prepareBuffer(index,4); |
|
182 |
return bb.getInt(index - (int) minAbs); |
|
183 |
} |
|
184 |
|
|
185 |
public synchronized long getLong(int index) { |
|
186 |
prepareBuffer(index,8); |
|
187 |
return bb.getLong(index - (int) minAbs); |
|
188 |
} |
|
189 |
|
|
190 |
public synchronized short getShort(int index) { |
|
191 |
prepareBuffer(index,2); |
|
192 |
return bb.getShort(index - (int) minAbs); |
|
193 |
} |
|
194 |
|
|
195 |
public ByteBuffer asReadOnlyBuffer() { |
|
196 |
return bb.asReadOnlyBuffer(); |
|
197 |
} |
|
198 |
|
|
199 |
public ByteBuffer compact() { |
|
200 |
return bb.compact(); |
|
201 |
} |
|
202 |
|
|
203 |
public ByteBuffer duplicate() { |
|
204 |
return bb.duplicate(); |
|
205 |
} |
|
206 |
|
|
207 |
public ByteBuffer slice() { |
|
208 |
return bb.slice(); |
|
209 |
} |
|
210 |
|
|
211 |
public synchronized ByteBuffer put(byte b) { |
|
212 |
prepareBuffer(posAbs,1); |
|
213 |
return bb.put(b); |
|
214 |
} |
|
215 |
|
|
216 |
public synchronized ByteBuffer putChar(char value) { |
|
217 |
prepareBuffer(posAbs,2); |
|
218 |
return bb.putChar(value); |
|
219 |
} |
|
220 |
|
|
221 |
public synchronized ByteBuffer putDouble(double value) { |
|
222 |
prepareBuffer(posAbs,8); |
|
223 |
return bb.putDouble(value); |
|
224 |
} |
|
225 |
|
|
226 |
public synchronized ByteBuffer putFloat(float value) { |
|
227 |
prepareBuffer(posAbs,4); |
|
228 |
return bb.putFloat(value); |
|
229 |
} |
|
230 |
|
|
231 |
public synchronized ByteBuffer putInt(int value) { |
|
232 |
prepareBuffer(posAbs,4); |
|
233 |
return bb.putInt(value); |
|
234 |
} |
|
235 |
|
|
236 |
public synchronized ByteBuffer put(int index, byte b) { |
|
237 |
prepareBuffer(index,1); |
|
238 |
return bb.put(index- (int) minAbs, b); |
|
239 |
} |
|
240 |
|
|
241 |
public synchronized ByteBuffer putChar(int index, char value) { |
|
242 |
prepareBuffer(index,2); |
|
243 |
return bb.putChar(index- (int) minAbs, value); |
|
244 |
} |
|
245 |
|
|
246 |
public synchronized ByteBuffer putDouble(int index, double value) { |
|
247 |
prepareBuffer(index,8); |
|
248 |
return bb.putDouble(index- (int) minAbs, value); |
|
249 |
} |
|
250 |
|
|
251 |
public synchronized ByteBuffer putFloat(int index, float value) { |
|
252 |
prepareBuffer(index,4); |
|
253 |
return bb.putFloat(index- (int) minAbs, value); |
|
254 |
} |
|
255 |
|
|
256 |
public synchronized ByteBuffer putInt(int index, int value) { |
|
257 |
prepareBuffer(index,4); |
|
258 |
return bb.putInt(index- (int) minAbs, value); |
|
259 |
} |
|
260 |
|
|
261 |
public synchronized ByteBuffer putLong(int index, long value) { |
|
262 |
prepareBuffer(index,8); |
|
263 |
return bb.putLong(index- (int) minAbs, value); |
|
264 |
} |
|
265 |
|
|
266 |
public synchronized ByteBuffer putShort(int index, short value) { |
|
267 |
prepareBuffer(index,2); |
|
268 |
return bb.putShort(index- (int) minAbs, value); |
|
269 |
} |
|
270 |
|
|
271 |
public synchronized ByteBuffer putLong(long value) { |
|
272 |
prepareBuffer(posAbs,8); |
|
273 |
return bb.putLong(value); |
|
274 |
} |
|
275 |
|
|
276 |
public synchronized ByteBuffer putShort(short value) { |
|
277 |
prepareBuffer(posAbs,2); |
|
278 |
return bb.putShort(value); |
|
279 |
} |
|
280 |
|
|
281 |
public CharBuffer asCharBuffer() { |
|
282 |
return bb.asCharBuffer(); |
|
283 |
} |
|
284 |
|
|
285 |
public DoubleBuffer asDoubleBuffer() { |
|
286 |
return bb.asDoubleBuffer(); |
|
287 |
} |
|
288 |
|
|
289 |
public FloatBuffer asFloatBuffer() { |
|
290 |
return bb.asFloatBuffer(); |
|
291 |
} |
|
292 |
|
|
293 |
public IntBuffer asIntBuffer() { |
|
294 |
return bb.asIntBuffer(); |
|
295 |
} |
|
296 |
|
|
297 |
public LongBuffer asLongBuffer() { |
|
298 |
return bb.asLongBuffer(); |
|
299 |
} |
|
300 |
|
|
301 |
public ShortBuffer asShortBuffer() { |
|
302 |
return bb.asShortBuffer(); |
|
303 |
} |
|
304 |
|
|
305 |
public boolean isReadOnly() { |
|
306 |
return bb.isReadOnly(); |
|
307 |
} |
|
308 |
|
|
309 |
public final ByteOrder order() |
|
310 |
{ |
|
311 |
return bb.order(); |
|
312 |
} |
|
313 |
|
|
314 |
public final ByteBuffer order(ByteOrder bo) |
|
315 |
{ |
|
316 |
return bb.order(bo); |
|
317 |
} |
|
318 |
public final long position() |
|
319 |
{ |
|
320 |
return posAbs; |
|
321 |
} |
|
322 |
|
|
323 |
public synchronized final Buffer position(long newPosition) |
|
324 |
{ |
|
325 |
prepareBuffer(newPosition,0); |
|
326 |
int relPos = (int) (newPosition - minAbs); |
|
327 |
// System.out.println("Position=" + newPosition); |
|
328 |
return bb.position(relPos); |
|
329 |
} |
|
330 |
|
|
331 |
} |
|
0 | 332 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/bigfile/TestBigByteBuffer.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
/* CVS MESSAGES: |
|
25 |
* |
|
26 |
* $Id: TestBigByteBuffer.java 29631 2009-06-29 16:56:19Z jpiera $ |
|
27 |
* $Log$ |
|
28 |
* Revision 1.3 2006-11-06 07:29:59 jaume |
|
29 |
* organize imports |
|
30 |
* |
|
31 |
* Revision 1.2 2006/05/12 12:40:29 fjp |
|
32 |
* Driver thread-safe. Se evitan errores aleatorios que aparec?an a veces. |
|
33 |
* |
|
34 |
* Revision 1.1 2006/01/18 13:01:08 fjp |
|
35 |
* BigByteBuffer2 que usa solo 8K y es adecuado para los ?ndices espaciales |
|
36 |
* |
|
37 |
* |
|
38 |
*/ |
|
39 |
package org.gvsig.utils.bigfile; |
|
40 |
|
|
41 |
import java.io.File; |
|
42 |
import java.io.FileInputStream; |
|
43 |
import java.io.FileNotFoundException; |
|
44 |
import java.io.IOException; |
|
45 |
import java.nio.ByteBuffer; |
|
46 |
import java.nio.channels.FileChannel; |
|
47 |
import java.util.Random; |
|
48 |
|
|
49 |
/** |
|
50 |
* @author fjp |
|
51 |
* |
|
52 |
*/ |
|
53 |
public class TestBigByteBuffer { |
|
54 |
File f = new File("D:/Fjp/chiara/plano/vias.shp"); |
|
55 |
FileInputStream fin; |
|
56 |
FileInputStream fin2; |
|
57 |
FileChannel channel2; |
|
58 |
|
|
59 |
BigByteBuffer2 bb; |
|
60 |
// Hacemos varias pruebas para ver si funciona |
|
61 |
// exactamente igual que el original |
|
62 |
static int numPruebas = 50000; |
|
63 |
|
|
64 |
public class MyThread extends Thread |
|
65 |
{ |
|
66 |
|
|
67 |
String name; |
|
68 |
public MyThread(String string) { |
|
69 |
name = string; |
|
70 |
|
|
71 |
} |
|
72 |
|
|
73 |
public void run() { |
|
74 |
try { |
|
75 |
prueba2(name, f, numPruebas); |
|
76 |
} catch (FileNotFoundException e) { |
|
77 |
// TODO Auto-generated catch block |
|
78 |
e.printStackTrace(); |
|
79 |
} catch (IOException e) { |
|
80 |
// TODO Auto-generated catch block |
|
81 |
e.printStackTrace(); |
|
82 |
} catch (Exception e) { |
|
83 |
e.printStackTrace(); |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* @param args |
|
91 |
* @throws IOException |
|
92 |
*/ |
|
93 |
public static void main(String[] args) throws IOException { |
|
94 |
// TODO Auto-generated method stub |
|
95 |
|
|
96 |
// prueba1(f, numPruebas); |
|
97 |
TestBigByteBuffer test = new TestBigByteBuffer(); |
|
98 |
test.test(); |
|
99 |
|
|
100 |
} |
|
101 |
|
|
102 |
public void test() throws IOException |
|
103 |
{ |
|
104 |
fin2 = new FileInputStream(f); |
|
105 |
channel2 = fin2.getChannel(); |
|
106 |
bb = new BigByteBuffer2(channel2, |
|
107 |
FileChannel.MapMode.READ_ONLY, 1024*8); |
|
108 |
MyThread th = new MyThread("T1:"); |
|
109 |
th.start(); |
|
110 |
MyThread th2 = new MyThread("T2: "); |
|
111 |
th2.start(); |
|
112 |
|
|
113 |
|
|
114 |
System.out.println("Fin de la prueba. " + numPruebas + " iteraciones."); |
|
115 |
|
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* @param name |
|
120 |
* @param f |
|
121 |
* @param numPruebas |
|
122 |
* @throws Exception |
|
123 |
*/ |
|
124 |
private void prueba2(String name, File f, int numPruebas) throws Exception { |
|
125 |
FileInputStream fin; |
|
126 |
fin = new FileInputStream(f); |
|
127 |
// Open the file and then get a channel from the stream |
|
128 |
FileChannel channel = fin.getChannel(); |
|
129 |
int size = (int) channel.size(); |
|
130 |
// Get the file's size and then map it into memory |
|
131 |
ByteBuffer bbCorrect = channel.map(FileChannel.MapMode.READ_ONLY, 0, size); |
|
132 |
// Chunkeo a 1KB |
|
133 |
Random rnd = new Random(); |
|
134 |
long t1 = System.currentTimeMillis(); |
|
135 |
|
|
136 |
for (int i=0; i < numPruebas; i++) |
|
137 |
{ |
|
138 |
int pos = rnd.nextInt(size-10); |
|
139 |
// pos = 0; |
|
140 |
bbCorrect.position(pos); |
|
141 |
int bCorrect = bbCorrect.getInt(); |
|
142 |
// bb.position(pos); |
|
143 |
int bPrueba = bb.getInt(pos); |
|
144 |
if (bCorrect != bPrueba) |
|
145 |
{ |
|
146 |
System.err.println(name + "Error de lectura. " + bCorrect + " " + bPrueba); |
|
147 |
throw new Exception("Error con pos=" + pos); |
|
148 |
} |
|
149 |
else |
|
150 |
{ |
|
151 |
System.out.println(name + "Correcto: pos=" + pos + " byte= " + bPrueba); |
|
152 |
} |
|
153 |
|
|
154 |
} |
|
155 |
close(channel2, fin2, bb); |
|
156 |
long t2 = System.currentTimeMillis(); |
|
157 |
System.out.println("T=" + (t2-t1) + "mseconds"); |
|
158 |
} |
|
159 |
|
|
160 |
/** |
|
161 |
* @param f |
|
162 |
* @param numPruebas |
|
163 |
* @throws FileNotFoundException |
|
164 |
* @throws IOException |
|
165 |
*/ |
|
166 |
private static void prueba1(File f, int numPruebas) throws FileNotFoundException, IOException { |
|
167 |
FileInputStream fin; |
|
168 |
fin = new FileInputStream(f); |
|
169 |
// Open the file and then get a channel from the stream |
|
170 |
FileChannel channel = fin.getChannel(); |
|
171 |
int size = (int) channel.size(); |
|
172 |
// Get the file's size and then map it into memory |
|
173 |
ByteBuffer bbCorrect = channel.map(FileChannel.MapMode.READ_ONLY, 0, size); |
|
174 |
// Chunkeo a 1KB |
|
175 |
Random rnd = new Random(); |
|
176 |
long t1 = System.currentTimeMillis(); |
|
177 |
FileInputStream fin2 = new FileInputStream(f); |
|
178 |
FileChannel channel2 = fin2.getChannel(); |
|
179 |
BigByteBuffer bb = new BigByteBuffer(channel2, |
|
180 |
FileChannel.MapMode.READ_ONLY, 1024*1024); |
|
181 |
for (int i=0; i < numPruebas; i++) |
|
182 |
{ |
|
183 |
int pos = rnd.nextInt(size-10); |
|
184 |
// pos = 2; |
|
185 |
|
|
186 |
byte bCorrect = bbCorrect.get(pos); |
|
187 |
byte bPrueba = bb.get(pos); |
|
188 |
if (bCorrect != bPrueba) |
|
189 |
{ |
|
190 |
System.err.println("Error de lectura. " + bCorrect + " " + bPrueba); |
|
191 |
} |
|
192 |
else |
|
193 |
{ |
|
194 |
// System.out.println("Correcto: pos=" + pos + " byte= " + bPrueba); |
|
195 |
} |
|
196 |
|
|
197 |
} |
|
198 |
close(channel2, fin2, bb); |
|
199 |
System.gc(); |
|
200 |
long t2 = System.currentTimeMillis(); |
|
201 |
System.out.println("T=" + (t2-t1) + "mseconds"); |
|
202 |
} |
|
203 |
|
|
204 |
static public synchronized void close(FileChannel channel, FileInputStream fin, BigByteBuffer2 bb) throws IOException { |
|
205 |
IOException ret = null; |
|
206 |
|
|
207 |
try { |
|
208 |
channel.close(); |
|
209 |
} catch (IOException e) { |
|
210 |
ret = e; |
|
211 |
} finally { |
|
212 |
try { |
|
213 |
fin.close(); |
|
214 |
} catch (IOException e1) { |
|
215 |
ret = e1; |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
if (ret != null) { |
|
220 |
throw ret; |
|
221 |
} |
|
222 |
// else // Si todo ha ido bien, preparamos para liberar memoria. |
|
223 |
// bb = null; |
|
224 |
} |
|
225 |
static public synchronized void close(FileChannel channel, FileInputStream fin, BigByteBuffer bb) throws IOException { |
|
226 |
IOException ret = null; |
|
227 |
|
|
228 |
try { |
|
229 |
channel.close(); |
|
230 |
} catch (IOException e) { |
|
231 |
ret = e; |
|
232 |
} finally { |
|
233 |
try { |
|
234 |
fin.close(); |
|
235 |
} catch (IOException e1) { |
|
236 |
ret = e1; |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
240 |
if (ret != null) { |
|
241 |
throw ret; |
|
242 |
} |
|
243 |
else // Si todo ha ido bien, preparamos para liberar memoria. |
|
244 |
bb = null; |
|
245 |
} |
|
246 |
|
|
247 |
} |
|
0 | 248 |
tags/org.gvsig.desktop-2.0.101/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/bigfile/BigByteBuffer2.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.utils.bigfile; |
|
25 |
|
|
26 |
import java.io.IOException; |
|
27 |
import java.nio.Buffer; |
|
28 |
import java.nio.ByteBuffer; |
|
29 |
import java.nio.ByteOrder; |
|
30 |
import java.nio.CharBuffer; |
|
31 |
import java.nio.DoubleBuffer; |
|
32 |
import java.nio.FloatBuffer; |
|
33 |
import java.nio.IntBuffer; |
|
34 |
import java.nio.LongBuffer; |
|
35 |
import java.nio.ShortBuffer; |
|
36 |
import java.nio.channels.FileChannel; |
|
37 |
|
|
38 |
/** |
|
39 |
* @author Fjp |
|
40 |
* |
|
41 |
* Clase para trabajar con ficheros grandes. Mapea solo un trozo |
|
42 |
* de fichero en memoria, y cuando intentas acceder fuera de esa |
|
43 |
* zona, se ocupa de leer autom?ticamente el trozo que le falta. |
|
44 |
*/ |
|
45 |
public class BigByteBuffer2 { |
|
46 |
|
|
47 |
private static long DEFAULT_SIZE = 60*1024; // 60 Kbytes |
|
48 |
|
|
49 |
ByteBuffer bb; |
|
50 |
// byte[] buff = new byte[1024 * 1024];; |
|
51 |
FileChannel fc; |
|
52 |
long minAbs, maxAbs, posAbs; |
|
53 |
int minRel, maxRel, posRel; |
|
54 |
long sizeChunk, amountMem; |
|
55 |
long fileSize; |
|
56 |
FileChannel.MapMode mode; |
|
57 |
|
|
58 |
/** |
|
59 |
* Revisa la posici?n absoluta, y si hace falta, carga el buffer |
|
60 |
* con la parte de fichero que toca. |
|
61 |
* @throws IOException |
|
62 |
*/ |
|
63 |
private synchronized void prepareBuffer(long posActual, int numBytesToRead) |
|
64 |
{ |
|
65 |
long desiredPos = posActual + numBytesToRead; |
|
66 |
if ((desiredPos > maxAbs) || (posActual < minAbs)) |
|
67 |
{ |
|
68 |
// Quiero leer fuera: |
|
69 |
sizeChunk = Math.min(fileSize-posActual, amountMem); |
|
70 |
try { |
|
71 |
mapFrom(posActual); |
|
72 |
// System.out.println("BigByteBuffer: min=" + minAbs |
|
73 |
// + " maxAbs=" + maxAbs + " posAbs = " + posAbs); |
|
74 |
|
|
75 |
} catch (IOException e) { |
|
76 |
e.printStackTrace(); |
|
77 |
|
|
78 |
} |
|
79 |
|
|
80 |
} |
|
81 |
// Dejamos posAbs apuntando a donde va a quedar |
|
82 |
// "a priori", antes de leer de verdad, que se hace |
|
83 |
// al salir de esta funci?n. |
|
84 |
posAbs = desiredPos; |
|
85 |
|
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* @param posActual |
|
90 |
* @throws IOException |
|
91 |
*/ |
|
92 |
private synchronized ByteBuffer mapFrom(long newPos) throws IOException { |
|
93 |
ByteOrder lastOrder = bb.order(); |
|
94 |
// bb = fc.map(mode, newPos, sizeChunk); |
|
95 |
fc.position(newPos); |
|
96 |
// bb = ByteBuffer.wrap(buff); |
|
97 |
// bb = ByteBuffer.allocate((int)sizeChunk); |
|
98 |
bb.position(0); |
|
99 |
int numRead = fc.read(bb); |
|
100 |
bb.position(0); |
|
101 |
// System.out.println("Mapeo desde " + newPos + " con sizeChunk= " + sizeChunk + " numRead = " + numRead); |
|
102 |
minAbs = newPos; |
|
103 |
maxAbs = sizeChunk + newPos; |
|
104 |
bb.order(lastOrder); |
|
105 |
return bb; |
|
106 |
} |
|
107 |
|
|
108 |
public BigByteBuffer2(FileChannel fc, FileChannel.MapMode mode, long amountMem) throws IOException |
|
109 |
{ |
|
110 |
this.amountMem = amountMem; |
|
111 |
// this.buff = new byte[(int) amountMem]; |
|
112 |
this.fc = fc; |
|
113 |
this.fileSize = fc.size(); |
|
114 |
this.mode = mode; |
|
115 |
sizeChunk = Math.min(fc.size(), amountMem); |
|
116 |
// bb = fc.map(mode, 0L, sizeChunk); |
|
117 |
// bb = ByteBuffer.wrap(buff); |
|
118 |
bb = ByteBuffer.allocateDirect((int)sizeChunk); |
|
119 |
int numRead = fc.read(bb); |
|
120 |
bb.position(0); |
|
121 |
minAbs = 0; |
|
122 |
maxAbs = sizeChunk; |
|
123 |
} |
|
124 |
public BigByteBuffer2(FileChannel fc, FileChannel.MapMode mode) throws IOException |
|
125 |
{ |
|
126 |
this.amountMem = DEFAULT_SIZE; |
|
127 |
this.fc = fc; |
|
128 |
this.fileSize = fc.size(); |
|
129 |
this.mode = mode; |
|
130 |
sizeChunk = Math.min(fc.size(), amountMem); |
|
131 |
// bb = fc.map(mode, 0L, sizeChunk); |
|
132 |
bb = ByteBuffer.allocateDirect((int)sizeChunk); |
|
133 |
int numRead = fc.read(bb); |
|
134 |
bb.position(0); |
|
135 |
minAbs = 0; |
|
136 |
maxAbs = sizeChunk; |
|
137 |
} |
|
138 |
|
|
139 |
|
|
140 |
public synchronized byte get() { |
|
141 |
prepareBuffer(posAbs,1); |
|
142 |
return bb.get(); |
|
143 |
} |
|
144 |
public synchronized ByteBuffer get(byte[] dst) |
|
145 |
{ |
|
146 |
prepareBuffer(posAbs, dst.length); |
|
147 |
return bb.get(dst); |
|
148 |
} |
|
149 |
|
|
150 |
public synchronized char getChar() { |
|
151 |
prepareBuffer(posAbs,2); |
|
152 |
return bb.getChar(); |
|
153 |
} |
|
154 |
|
|
155 |
public synchronized double getDouble() { |
|
156 |
prepareBuffer(posAbs,8); |
|
157 |
return bb.getDouble(); |
|
158 |
} |
|
159 |
|
|
160 |
public synchronized float getFloat() { |
|
161 |
prepareBuffer(posAbs,4); |
|
162 |
return bb.getFloat(); |
|
163 |
} |
|
164 |
|
|
165 |
public synchronized int getInt() { |
|
166 |
prepareBuffer(posAbs,4); |
|
167 |
return bb.getInt(); |
|
168 |
} |
|
169 |
|
|
170 |
public synchronized long getLong() { |
|
171 |
prepareBuffer(posAbs,8); |
|
172 |
return bb.getLong(); |
|
173 |
} |
|
174 |
|
|
175 |
public synchronized short getShort() { |
|
176 |
prepareBuffer(posAbs,2); |
|
177 |
return bb.getShort(); |
|
178 |
} |
|
179 |
|
|
180 |
public boolean isDirect() { |
|
181 |
return bb.isDirect(); |
|
182 |
} |
|
183 |
|
|
184 |
public synchronized byte get(int index) { |
|
185 |
prepareBuffer(index,1); |
|
186 |
return bb.get(index - (int) minAbs); |
|
187 |
} |
|
188 |
|
|
189 |
public synchronized char getChar(int index) { |
|
190 |
prepareBuffer(index,2); |
|
191 |
return bb.getChar(index - (int) minAbs); |
|
192 |
} |
|
193 |
|
|
194 |
public synchronized double getDouble(int index) { |
|
195 |
prepareBuffer(index,8); |
|
196 |
return bb.getDouble(index - (int) minAbs); |
|
197 |
} |
|
198 |
|
|
199 |
public synchronized float getFloat(int index) { |
|
200 |
prepareBuffer(index,4); |
|
201 |
return bb.getFloat(index - (int) minAbs); |
|
202 |
} |
|
203 |
|
|
204 |
public synchronized int getInt(int index) { |
|
205 |
prepareBuffer(index,4); |
|
206 |
return bb.getInt(index - (int) minAbs); |
|
207 |
} |
|
208 |
|
|
209 |
public synchronized long getLong(int index) { |
|
210 |
prepareBuffer(index,8); |
|
211 |
return bb.getLong(index - (int) minAbs); |
|
212 |
} |
|
213 |
|
|
214 |
public synchronized short getShort(int index) { |
|
215 |
prepareBuffer(index,2); |
|
216 |
return bb.getShort(index - (int) minAbs); |
|
217 |
} |
|
218 |
|
|
219 |
public ByteBuffer asReadOnlyBuffer() { |
|
220 |
return bb.asReadOnlyBuffer(); |
|
221 |
} |
|
222 |
|
|
223 |
public ByteBuffer compact() { |
|
224 |
return bb.compact(); |
|
225 |
} |
|
226 |
|
|
227 |
public ByteBuffer duplicate() { |
|
228 |
return bb.duplicate(); |
|
229 |
} |
|
230 |
|
|
231 |
public synchronized ByteBuffer slice() { |
|
232 |
return bb.slice(); |
|
233 |
} |
|
234 |
|
|
235 |
public synchronized ByteBuffer put(byte b) { |
|
236 |
prepareBuffer(posAbs,1); |
|
237 |
return bb.put(b); |
|
238 |
} |
|
239 |
|
|
240 |
public synchronized ByteBuffer putChar(char value) { |
|
241 |
prepareBuffer(posAbs,2); |
|
242 |
return bb.putChar(value); |
|
243 |
} |
|
244 |
|
|
245 |
public synchronized ByteBuffer putDouble(double value) { |
|
246 |
prepareBuffer(posAbs,8); |
|
247 |
return bb.putDouble(value); |
|
248 |
} |
|
249 |
|
|
250 |
public synchronized ByteBuffer putFloat(float value) { |
|
251 |
prepareBuffer(posAbs,4); |
|
252 |
return bb.putFloat(value); |
|
253 |
} |
|
254 |
|
|
255 |
public synchronized ByteBuffer putInt(int value) { |
|
256 |
prepareBuffer(posAbs,4); |
|
257 |
return bb.putInt(value); |
|
258 |
} |
|
259 |
|
|
260 |
public synchronized ByteBuffer put(int index, byte b) { |
|
261 |
prepareBuffer(index,1); |
|
262 |
return bb.put(index- (int) minAbs, b); |
|
263 |
} |
|
264 |
|
|
265 |
public synchronized ByteBuffer putChar(int index, char value) { |
|
266 |
prepareBuffer(index,2); |
|
267 |
return bb.putChar(index- (int) minAbs, value); |
|
268 |
} |
|
269 |
|
|
270 |
public synchronized ByteBuffer putDouble(int index, double value) { |
|
271 |
prepareBuffer(index,8); |
|
272 |
return bb.putDouble(index- (int) minAbs, value); |
|
273 |
} |
|
274 |
|
|
275 |
public synchronized ByteBuffer putFloat(int index, float value) { |
|
276 |
prepareBuffer(index,4); |
|
277 |
return bb.putFloat(index- (int) minAbs, value); |
|
278 |
} |
|
279 |
|
|
280 |
public synchronized ByteBuffer putInt(int index, int value) { |
|
281 |
prepareBuffer(index,4); |
|
282 |
return bb.putInt(index- (int) minAbs, value); |
|
283 |
} |
|
284 |
|
|
285 |
public synchronized ByteBuffer putLong(int index, long value) { |
|
286 |
prepareBuffer(index,8); |
|
287 |
return bb.putLong(index- (int) minAbs, value); |
|
288 |
} |
|
289 |
|
|
290 |
public synchronized ByteBuffer putShort(int index, short value) { |
|
291 |
prepareBuffer(index,2); |
|
292 |
return bb.putShort(index- (int) minAbs, value); |
|
293 |
} |
|
294 |
|
|
295 |
public synchronized ByteBuffer putLong(long value) { |
|
296 |
prepareBuffer(posAbs,8); |
|
297 |
return bb.putLong(value); |
|
298 |
} |
|
299 |
|
|
300 |
public synchronized ByteBuffer putShort(short value) { |
|
301 |
prepareBuffer(posAbs,2); |
|
302 |
return bb.putShort(value); |
|
303 |
} |
|
304 |
|
|
305 |
public CharBuffer asCharBuffer() { |
|
306 |
return bb.asCharBuffer(); |
|
307 |
} |
|
308 |
|
|
309 |
public DoubleBuffer asDoubleBuffer() { |
|
310 |
return bb.asDoubleBuffer(); |
|
311 |
} |
|
312 |
|
|
313 |
public FloatBuffer asFloatBuffer() { |
|
314 |
return bb.asFloatBuffer(); |
|
315 |
} |
|
316 |
|
|
317 |
public IntBuffer asIntBuffer() { |
|
318 |
return bb.asIntBuffer(); |
|
319 |
} |
|
320 |
|
|
321 |
public LongBuffer asLongBuffer() { |
|
322 |
return bb.asLongBuffer(); |
|
323 |
} |
|
324 |
|
|
325 |
public ShortBuffer asShortBuffer() { |
|
326 |
return bb.asShortBuffer(); |
|
327 |
} |
|
328 |
|
|
329 |
public boolean isReadOnly() { |
|
330 |
return bb.isReadOnly(); |
|
331 |
} |
|
332 |
|
|
333 |
public synchronized final ByteOrder order() |
|
334 |
{ |
|
335 |
return bb.order(); |
|
336 |
} |
|
337 |
|
|
338 |
public synchronized final ByteBuffer order(ByteOrder bo) |
|
339 |
{ |
|
340 |
return bb.order(bo); |
|
341 |
} |
|
342 |
public synchronized final long position() |
|
343 |
{ |
|
344 |
return posAbs; |
|
345 |
} |
|
346 |
|
|
347 |
public synchronized final Buffer position(long newPosition) |
|
348 |
{ |
|
349 |
prepareBuffer(newPosition,0); |
|
350 |
int relPos = (int) (newPosition - minAbs); |
|
351 |
if (relPos < 0) |
|
352 |
System.out.println("Position=" + newPosition); |
|
353 |
return bb.position(relPos); |
|
354 |
} |
|
355 |
|
|
356 |
} |
|
0 | 357 |
Also available in: Unified diff