什么是clazz用于"私有静态类clazz = SnappyDecompressor.class"源文件?

时间:2016-10-15 19:41:36

标签: java hadoop java-native-interface native

我正在研究Snappy,Zlib和其他人的压缩器实现(用Java)。在源文件的顶部附近是下面这一行。任何人都可以向我解释这是什么意思吗?

HACK - Use this as a global lock in the JNI layer
@SuppressWarnings({"unchecked", "unused"})
private static Class clazz = SnappyDecompressor.class;

据我所知,例如,在Snappy中,SnappyDecompressor.java本质上是snappy C / native实现的包装器,为了调用该C实现,Java层通过JNI接口进行调用。

snappy(Java包装器)源代码的顶部,SnappyDecompressor.java在这里:

package org.apache.hadoop.io.compress.snappy;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.compress.Decompressor;

/**
 * A {@link Decompressor} based on the snappy compression algorithm.
 * http://code.google.com/p/snappy/
 */
public class SnappyDecompressor implements Decompressor {
  private static final Log LOG =
      LogFactory.getLog(SnappyCompressor.class.getName());
  private static final int DEFAULT_DIRECT_BUFFER_SIZE = 64 * 1024;

  // HACK - Use this as a global lock in the JNI layer
  @SuppressWarnings({"unchecked", "unused"})
  private static Class clazz = SnappyDecompressor.class;

Full source

1 个答案:

答案 0 :(得分:0)

如评论中所示:它是从JNI层使用的,似乎是对JNI层的某些锁定。 (see the full Decompressor source

第76,77行:

SnappyDecompressor_clazz = (*env)->GetStaticFieldID(env, clazz, "clazz",
                                                 "Ljava/lang/Class;");

第83行:

jobject clazz = (*env)->GetStaticObjectField(env,thisj, SnappyDecompressor_clazz);

第100至102行:

LOCK_CLASS(env, clazz, "SnappyDecompressor");
const char* compressed_bytes = (const char*)(*env)->GetDirectBufferAddress(env, compressed_direct_buf);
UNLOCK_CLASS(env, clazz, "SnappyDecompressor");