Creating opaque data types from Java

Does anyone have a working code example that creates an opaque datatype and a dataset of that type using the HDF Object Package (high-level Java interface)? I get nothing but mysterious exceptions, whatever I try.

Thanks in advance,
   Konrad.

···

--
---------------------------------------------------------------------
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: research at khinsen dot fastmail dot net
---------------------------------------------------------------------

You may have to use the Java wrapper. Below is the sample code that writes any
data (pdf, excel, tiff, video, etc) into HDF5 using opaque.

···

===============
     private boolean createH5Dataset(int h5fid, String h5dsetName, String srcFilename)
     throws Exception {
         int did = -1, tid = -1, sid = -1;
         long size = (new File(srcFilename)).length();
         long dims[] = {size};

         if (size <=0)
             return false; // nothing to write

         tid = H5.H5Tcreate (HDF5Constants.H5T_OPAQUE, 1);
         H5.H5Tset_tag (tid, "Content-Type: application/pdf");
         //H5.H5Tset_tag (tid, "Content-Type: application/vnd.ms-excel");
         // H5.H5Tset_tag (tid, "Content-Type: video/mp4");
         sid = H5.H5Screate_simple (1, dims, null);

         did = H5.H5Dcreate (h5fid, h5dsetName, tid, sid, HDF5Constants.H5P_DEFAULT);

         BufferedInputStream bufferedInput = null;
         byte[] buffer = new byte[(int)size];

         bufferedInput = new BufferedInputStream(new FileInputStream(srcFilename));
         bufferedInput.read(buffer);

         H5.H5Dwrite (did, tid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, buffer);

         try {
             H5.H5Tclose(tid);
         } catch (HDF5Exception ex) {
         }
         try {
             H5.H5Sclose(sid);
         } catch (HDF5Exception ex) {
         }
         try {
             H5.H5Dclose(did);
         } catch (HDF5Exception ex) {
         }

         return (did > 0);
     }

         return (did > 0);
     }

On 3/22/2011 8:53 AM, Konrad Hinsen wrote:

Does anyone have a working code example that creates an opaque datatype and a dataset of that type using the HDF Object Package (high-level Java interface)? I get nothing but mysterious exceptions, whatever I try.

Thanks in advance,
  Konrad.

Thanks, that is pretty close to my use case, so I could adapt it easily!

Konrad.

···

On 22/03/2011 15:30, Peter Cao wrote:

You may have to use the Java wrapper. Below is the sample code that
writes any
data (pdf, excel, tiff, video, etc) into HDF5 using opaque.

--
---------------------------------------------------------------------
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: research at khinsen dot fastmail dot net
---------------------------------------------------------------------

My next problem with the high-level Java API is creating variable-length strings, either in datasets or as attribute values. Nothing but strange error messages... To start with, if I create a H5Datatype of CLASS_VLEN and ask for its description, it is "unknown". Any use of such a datatype leads to exceptions. Does anyone have a working example?

Thanks in advance,
   Konrad.

···

--
---------------------------------------------------------------------
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: research at khinsen dot fastmail dot net
---------------------------------------------------------------------

Below are two exmaples:
testH5Vlen() uses the HDF5 Java wrapper
testH5VlenObj() uses the object level (high level)

···

=================
     private static void testH5Vlen(final String filename) throws Exception
     {
         String buf[] = {"Parting", "is such", "sweet", "sorrow."};

         // Case 1, may run into infinite loop
         // int tid = H5.H5Tvlen_create(HDF5Constants.H5T_C_S1);

         // Case 2, differnt failure on differnt platforms
         int tid = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
         H5.H5Tset_size(tid, HDF5Constants.H5T_VARIABLE);

         int fid = H5.H5Fcreate(filename, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
         int sid = H5.H5Screate_simple(1, new long[] {4}, null);
         int did = H5.H5Dcreate(fid, "/str", tid, sid, HDF5Constants.H5P_DEFAULT);

         // write() fails on both case 1 and 2
         H5.H5Dwrite(did, tid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, buf);

         // clean up
         H5.H5Dclose(did);
         H5.H5Sclose(sid);
         H5.H5Tclose(tid);
         H5.H5Fclose(fid);
     }

     private static void testH5VlenObj(final String fname) throws Exception
     {
         int strLen = -1;
         long[] dims = {4};
         String buf[] = {"Parting", "is such", "sweet", "sorrow."};

         // create a new file with a given file name.
         H5File testFile = new H5File(fname, H5File.CREATE);

         testFile.open();
         Group root = (Group)((javax.swing.tree.DefaultMutableTreeNode)testFile.getRootNode()).getUserObject();
         Datatype dtype = testFile.createDatatype(Datatype.CLASS_STRING, strLen, Datatype.NATIVE, Datatype.NATIVE);
         Dataset dataset = testFile.createScalarDS ("/str", root, dtype, dims, null, null, 0, buf);

         testFile.close();
      }

On 3/24/2011 11:54 AM, Konrad Hinsen wrote:

My next problem with the high-level Java API is creating variable-length strings, either in datasets or as attribute values. Nothing but strange error messages... To start with, if I create a H5Datatype of CLASS_VLEN and ask for its description, it is "unknown". Any use of such a datatype leads to exceptions. Does anyone have a working example?

Thanks in advance,
  Konrad.

I haven't used the java high level interface, but you should get some joy if you look here: http://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/api16-java.html

In particular, look at

http://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/java/examples/datatypes/H5Ex_T_String.java

and http://www.hdfgroup.org/HDF5/doc/H5.intro.html#Intro-PMCreateVariableLength

In C++, you create a HD5::StrType, and give H5T_VARIABLE as the length. There's a good example here:

···

On 3/24/11 11:54 AM, "Konrad Hinsen" <research@khinsen.fastmail.net<mailto:research@khinsen.fastmail.net>> wrote:

My next problem with the high-level Java API is creating variable-length
strings, either in datasets or as attribute values. Nothing but strange
error messages... To start with, if I create a H5Datatype of CLASS_VLEN
and ask for its description, it is "unknown". Any use of such a datatype
leads to exceptions. Does anyone have a working example?

Thanks in advance,
   Konrad.
--
---------------------------------------------------------------------
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: research at khinsen dot fastmail dot net
---------------------------------------------------------------------

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org<mailto:Hdf-forum@hdfgroup.org>
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error, then delete it. Thank you.
________________________________

Thanks, this works fine! So the trick is to use CLASS_STRING with a length of -1, rather than CLASS_VLEN as I assumed. Does CLASS_VLEN have any use then? Could something about variable strings be added to the documentation?

Konrad

···

On 24/3/11 18:36 , Peter Cao wrote:

Below are two exmaples:
testH5Vlen() uses the HDF5 Java wrapper
testH5VlenObj() uses the object level (high level)

--
---------------------------------------------------------------------
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: research at khinsen dot fastmail dot net
---------------------------------------------------------------------

Hi people..
i am very new to hdf5
i have an object (message is the name of the object in the given code)which
carries a serialized text.
i want to write it to hdf5 file using opaque data type specifically.
how do i go about doing it?

private static int count = 0;
  public static void record(Message message)
  {
  try
    {
    
    BufferedOutputStream buf=new BufferedOutputStream(new
FileOutputStream("E:/proj/"+count+".bin"));
    byte[] b =serializer.serialize(message);
      buf.write(b);
      buf.flush();
      count++;
          
      }
    catch(Exception e){}
  
  }

···

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Creating-opaque-data-types-from-Java-tp2714926p3803053.html
Sent from the hdf-forum mailing list archive at Nabble.com.

attached is an example of writing a pdf file to an HDF5 dataset of opaque.

PDF2H5.java (5.84 KB)

···

On 3/6/2012 2:54 AM, kuki wrote:

Hi people..
i am very new to hdf5
i have an object (message is the name of the object in the given code)which
carries a serialized text.
i want to write it to hdf5 file using opaque data type specifically.
how do i go about doing it?

private static int count = 0;
  public static void record(Message message)
  {
  try
    {
    
    BufferedOutputStream buf=new BufferedOutputStream(new
FileOutputStream("E:/proj/"+count+".bin"));
    byte[] b =serializer.serialize(message);
       buf.write(b);
       buf.flush();
      count++;
          
       }
     catch(Exception e){}
  
   }

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Creating-opaque-data-types-from-Java-tp2714926p3803053.html
Sent from the hdf-forum mailing list archive at Nabble.com.

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

--
Thank you!
--pc