1 /* Copyright (C) 2006, 2007 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 #include <java/net/VMURLConnection.h>
13 #include <java/lang/UnsupportedOperationException.h>
16 #if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
21 static magic_t cookie
;
23 static magic_t (*p_magic_open
)(int flags
);
24 static int (*p_magic_load
)(magic_t cookie
, const char *filename
);
25 static void (*p_magic_close
)(magic_t cookie
);
26 static const char * (*p_magic_buffer
) (magic_t cookie
, const void *buffer
,
29 #endif /* HAVE_MAGIC_H && defined (USE_LTDL) */
32 java::net::VMURLConnection::init ()
34 #if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
35 lt_dlhandle handle
= lt_dlopenext ("libmagic.so");
39 p_magic_open
= (typeof (p_magic_open
))lt_dlsym(handle
, "magic_open");
40 if (p_magic_open
== NULL
)
42 p_magic_buffer
= (typeof (p_magic_buffer
))lt_dlsym(handle
, "magic_buffer");
43 if (p_magic_buffer
== NULL
)
45 p_magic_close
= (typeof (p_magic_close
))lt_dlsym(handle
, "magic_close");
46 if (p_magic_close
== NULL
)
48 p_magic_load
= (typeof (p_magic_load
))lt_dlsym(handle
, "magic_load");
49 if (p_magic_load
== NULL
)
52 cookie
= p_magic_open (MAGIC_MIME
);
53 if (cookie
== (magic_t
) NULL
)
55 if (p_magic_load (cookie
, NULL
) == -1)
57 p_magic_close (cookie
);
58 cookie
= (magic_t
) NULL
;
60 #endif /* HAVE_MAGIC_H && defined (USE_LTDL) */
63 ::java::lang::String
*
64 java::net::VMURLConnection::guessContentTypeFromBuffer (jbyteArray bytes
,
67 #if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
70 if (cookie
== (magic_t
) NULL
)
73 result
= p_magic_buffer (cookie
, elements(bytes
), valid
);
77 return _Jv_NewStringUTF (result
);
80 #endif /* HAVE_MAGIC_H && defined (USE_LTDL) */