1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * Copyright by The HDF Group. * 3 * Copyright by the Board of Trustees of the University of Illinois. * 4 * All rights reserved. * 5 * * 6 * This file is part of HDF5. The full HDF5 copyright notice, including * 7 * terms governing use, modification, and redistribution, is contained in * 8 * the COPYING file, which can be found at the root of the source code * 9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. * 10 * If you do not have access to either file, you may request a copy from * 11 * help@hdfgroup.org. * 12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 13 14 /************************************************************ 15 Creating and writing a VL string to a file. 16 ************************************************************/ 17 18 package examples.datatypes; 19 20 import hdf.hdf5lib.H5; 21 import hdf.hdf5lib.HDF5Constants; 22 23 public class H5Ex_T_VLString 24 { 25 private static String FILENAME = "H5Ex_T_VLString.h5"; 26 private static String DATASETNAME = "DS1"; 27 createDataset()28 private static void createDataset() { 29 long file_id = -1; 30 long type_id = -1; 31 long dataspace_id = -1; 32 long dataset_id = -1; 33 int rank = 1; 34 String[] str_data = { "Parting", "is such", "sweet", "sorrow." }; 35 long[] dims = { str_data.length }; 36 37 // Create a new file using default properties. 38 try { 39 file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, 40 HDF5Constants.H5P_DEFAULT); 41 } 42 catch (Exception e) { 43 e.printStackTrace(); 44 } 45 46 try { 47 type_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); 48 H5.H5Tset_size(type_id, HDF5Constants.H5T_VARIABLE); 49 } 50 catch (Exception e) { 51 e.printStackTrace(); 52 } 53 54 // Create dataspace. Setting maximum size to NULL sets the maximum 55 // size to be the current size. 56 try { 57 dataspace_id = H5.H5Screate_simple(rank, dims, null); 58 } 59 catch (Exception e) { 60 e.printStackTrace(); 61 } 62 63 // Create the dataset and write the string data to it. 64 try { 65 if ((file_id >= 0) && (type_id >= 0) && (dataspace_id >= 0)) { 66 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, type_id, dataspace_id, HDF5Constants.H5P_DEFAULT, 67 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); 68 } 69 } 70 catch (Exception e) { 71 e.printStackTrace(); 72 } 73 74 // Write the data to the dataset. 75 try { 76 if (dataset_id >= 0) 77 H5.H5Dwrite_VLStrings(dataset_id, type_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 78 HDF5Constants.H5P_DEFAULT, str_data); 79 } 80 catch (Exception e) { 81 e.printStackTrace(); 82 } 83 84 try { 85 H5.H5Sclose(dataspace_id); 86 H5.H5Tclose(type_id); 87 H5.H5Dclose(dataset_id); 88 H5.H5Fclose(file_id); 89 } 90 catch (Exception e) { 91 e.printStackTrace(); 92 } 93 } 94 readDataset()95 private static void readDataset() { 96 long file_id = -1; 97 long type_id = -1; 98 long dataset_id = -1; 99 String[] str_data = { "", "", "", "" }; 100 101 try { 102 file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); 103 } 104 catch (Exception e) { 105 e.printStackTrace(); 106 } 107 108 try { 109 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); 110 type_id = H5.H5Dget_type(dataset_id); 111 H5.H5Dread_VLStrings(dataset_id, type_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, 112 str_data); 113 } 114 catch (Exception e) { 115 e.printStackTrace(); 116 } 117 118 for (int indx = 0; indx < str_data.length; indx++) 119 System.out.println(DATASETNAME + " [" + indx + "]: " + str_data[indx]); 120 121 try { 122 H5.H5Tclose(type_id); 123 H5.H5Dclose(dataset_id); 124 H5.H5Fclose(file_id); 125 } 126 catch (Exception e) { 127 e.printStackTrace(); 128 } 129 } 130 main(String[] args)131 public static void main(String[] args) { 132 H5Ex_T_VLString.createDataset(); 133 H5Ex_T_VLString.readDataset(); 134 } 135 136 } 137