aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/natVMURLConnection.cc
blob: 3429bb63dba44bed4b5008a4e50e4bd986c6e612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Copyright (C) 2006  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>

#include <java/net/VMURLConnection.h>
#include <gcj/cni.h>
#include <java/lang/UnsupportedOperationException.h>

#if defined (HAVE_MAGIC_H) && defined (HAVE_MAGIC_OPEN)

#include <magic.h>

static magic_t cookie;

#endif /* HAVE_MAGIC_H && HAVE_MAGIC_OPEN */

void
java::net::VMURLConnection::init ()
{
#if defined (HAVE_MAGIC_H) && defined (HAVE_MAGIC_OPEN)
  cookie = magic_open (MAGIC_MIME);
  if (cookie == (magic_t) NULL)
    return;
  if (magic_load (cookie, NULL) == -1)
    {
      magic_close (cookie);
      cookie = (magic_t) NULL;
    }
#endif /* HAVE_MAGIC_H && HAVE_MAGIC_OPEN */
}

::java::lang::String *
java::net::VMURLConnection::guessContentTypeFromBuffer (jbyteArray bytes,
							jint valid)
{
#if defined (HAVE_MAGIC_H) && defined (HAVE_MAGIC_OPEN)
  const char *result;

  if (cookie == (magic_t) NULL)
    return NULL;

  result = magic_buffer (cookie, elements(bytes), valid);

  if (result == NULL)
    return NULL;
  return _Jv_NewStringUTF (result);
#else
  return NULL;
#endif /* HAVE_MAGIC_H && HAVE_MAGIC_OPEN */
}