1/* Copyright 2015 Google Inc. All Rights Reserved. 2 3 Distributed under MIT license. 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5*/ 6 7package org.brotli.dec; 8 9import java.nio.ByteBuffer; 10 11/** 12 * Collection of static dictionary words. 13 * 14 * <p>Dictionary content is loaded from binary resource when {@link #getData()} is executed for the 15 * first time. Consequently, it saves memory and CPU in case dictionary is not required. 16 * 17 * <p>One possible drawback is that multiple threads that need dictionary data may be blocked (only 18 * once in each classworld). To avoid this, it is enough to call {@link #getData()} proactively. 19 */ 20public final class Dictionary { 21 private static volatile ByteBuffer data; 22 23 private static class DataLoader { 24 static final boolean OK; 25 26 static { 27 boolean ok = true; 28 try { 29 Class.forName(Dictionary.class.getPackage().getName() + ".DictionaryData"); 30 } catch (Throwable ex) { 31 ok = false; 32 } 33 OK = ok; 34 } 35 } 36 37 public static void setData(ByteBuffer data) { 38 if (!data.isDirect() || !data.isReadOnly()) { 39 throw new BrotliRuntimeException("data must be a direct read-only byte buffer"); 40 } 41 Dictionary.data = data; 42 } 43 44 public static ByteBuffer getData() { 45 if (data != null) { 46 return data; 47 } 48 if (!DataLoader.OK) { 49 throw new BrotliRuntimeException("brotli dictionary is not set"); 50 } 51 /* Might have been set when {@link DictionaryData} was loaded.*/ 52 return data; 53 } 54} 55