| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.samples; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.RandomAccessFile; |
| 10 |
import java.sql.Connection; |
| 11 |
import java.sql.DriverManager; |
| 12 |
import java.sql.ResultSet; |
| 13 |
import java.sql.Statement; |
| 14 |
|
| 15 |
/** |
| 16 |
* This sample application shows how to create a user defined function |
| 17 |
* to read a file from the file system. |
| 18 |
*/ |
| 19 |
public class FileFunctions { |
| 20 |
|
| 21 |
/** |
| 22 |
* This method is called when executing this sample application from the |
| 23 |
* command line. |
| 24 |
* |
| 25 |
* @param args the command line parameters |
| 26 |
* @throws Exception on failure |
| 27 |
*/ |
| 28 |
public static void main(String... args) throws Exception { |
| 29 |
Class.forName("org.h2.Driver"); |
| 30 |
Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", ""); |
| 31 |
Statement stat = conn.createStatement(); |
| 32 |
stat.execute("CREATE ALIAS READ_TEXT_FILE FOR 'org.h2.samples.FileFunctions.readTextFile'"); |
| 33 |
stat.execute("CREATE ALIAS READ_TEXT_FILE_WITH_ENCODING " + |
| 34 |
"FOR 'org.h2.samples.FileFunctions.readTextFileWithEncoding'"); |
| 35 |
stat.execute("CREATE ALIAS READ_FILE FOR 'org.h2.samples.FileFunctions.readFile'"); |
| 36 |
ResultSet rs = stat.executeQuery("CALL READ_FILE('test.txt')"); |
| 37 |
rs.next(); |
| 38 |
byte[] data = rs.getBytes(1); |
| 39 |
System.out.println("length: " + data.length); |
| 40 |
rs = stat.executeQuery("CALL READ_TEXT_FILE('test.txt')"); |
| 41 |
rs.next(); |
| 42 |
String text = rs.getString(1); |
| 43 |
System.out.println("text: " + text); |
| 44 |
stat.close(); |
| 45 |
conn.close(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Read a String from a file. The default encoding for this platform is |
| 50 |
* used. |
| 51 |
* |
| 52 |
* @param fileName the file name |
| 53 |
* @return the text |
| 54 |
* @throws IOException on failure |
| 55 |
*/ |
| 56 |
public static String readTextFile(String fileName) throws IOException { |
| 57 |
byte[] buff = readFile(fileName); |
| 58 |
String s = new String(buff); |
| 59 |
return s; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Read a String from a file using the specified encoding. |
| 64 |
* |
| 65 |
* @param fileName the file name |
| 66 |
* @param encoding the encoding |
| 67 |
* @return the text |
| 68 |
* @throws IOException on failure |
| 69 |
*/ |
| 70 |
public static String readTextFileWithEncoding(String fileName, |
| 71 |
String encoding) throws IOException { |
| 72 |
byte[] buff = readFile(fileName); |
| 73 |
String s = new String(buff, encoding); |
| 74 |
return s; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Read a file into a byte array. |
| 79 |
* |
| 80 |
* @param fileName the file name |
| 81 |
* @return the byte array |
| 82 |
* @throws IOException on failure |
| 83 |
*/ |
| 84 |
public static byte[] readFile(String fileName) throws IOException { |
| 85 |
try (RandomAccessFile file = new RandomAccessFile(fileName, "r")) { |
| 86 |
byte[] buff = new byte[(int) file.length()]; |
| 87 |
file.readFully(buff); |
| 88 |
return buff; |
| 89 |
} |
| 90 |
} |
| 91 |
} |