configure, [...]: Rebuilt.
[official-gcc.git] / libjava / java / net / natVMURLConnection.cc
blob643f90a978ca4f4a371ac995028687f154874a91
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
7 details. */
9 #include <config.h>
11 #include <java/net/VMURLConnection.h>
12 #include <gcj/cni.h>
13 #include <java/lang/UnsupportedOperationException.h>
14 #include <stdio.h>
16 #if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
18 #include <magic.h>
19 #include <ltdl.h>
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,
27 size_t length);
29 #endif /* HAVE_MAGIC_H && defined (USE_LTDL) */
31 void
32 java::net::VMURLConnection::init ()
34 #if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
35 lt_dlhandle handle = lt_dlopenext ("libmagic.so");
36 if (!handle)
37 return;
39 p_magic_open = (typeof (p_magic_open))lt_dlsym(handle, "magic_open");
40 if (p_magic_open == NULL)
41 return;
42 p_magic_buffer = (typeof (p_magic_buffer))lt_dlsym(handle, "magic_buffer");
43 if (p_magic_buffer == NULL)
44 return;
45 p_magic_close = (typeof (p_magic_close))lt_dlsym(handle, "magic_close");
46 if (p_magic_close == NULL)
47 return;
48 p_magic_load = (typeof (p_magic_load))lt_dlsym(handle, "magic_load");
49 if (p_magic_load == NULL)
50 return;
52 cookie = p_magic_open (MAGIC_MIME);
53 if (cookie == (magic_t) NULL)
54 return;
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,
65 jint valid)
67 #if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
68 const char *result;
70 if (cookie == (magic_t) NULL)
71 return NULL;
73 result = p_magic_buffer (cookie, elements(bytes), valid);
75 if (result == NULL)
76 return NULL;
77 return _Jv_NewStringUTF (result);
78 #else
79 return NULL;
80 #endif /* HAVE_MAGIC_H && defined (USE_LTDL) */