所有现代浏览器都包含用于与服务器交换压缩数据的gzip例程.任何人都可以指出我正确的方向来编写一个允许Javascript利用这个例程的Chrome扩展吗?
我希望在通过WebSocket将其发送到服务器之前在Javascript中压缩一些数据,而Chrome内置的deflate例程肯定比我在Javascript中编写的任何内容都要快.
javascript gzip google-chrome websocket google-chrome-extension
以下代码抛出一个NullPointerException.
import java.io.*;
public class NullFinalTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Foo foo = new Foo();
foo.useLock();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
new ObjectOutputStream(buffer).writeObject(foo);
foo = (Foo) new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())).readObject();
foo.useLock();
}
public static class Foo implements Serializable {
private final String lockUsed = "lock used";
private transient final Object lock = new Object();
public void useLock() {
System.out.println("About to synchronize");
synchronized (lock) { // <- NullPointerException here on 2nd call
System.out.println(lockUsed);
} …Run Code Online (Sandbox Code Playgroud) 我的目标是实现一个方法,将任意数量的数组连接到它们的公共超类型的单个数组中,返回生成的(类型化的)数组.我有两个实现.
第一个(这个不需要简化):
public static <T> T[] concatArrays(Class<T> type, T[]... arrays) {
int totalLen = 0;
for (T[] arr: arrays) {
arr.getClass().getCom
totalLen += arr.length;
}
T[] all = (T[]) Array.newInstance(type, totalLen);
int copied = 0;
for (T[] arr: arrays) {
System.arraycopy(arr, 0, all, copied, arr.length);
copied += arr.length;
}
return all;
}
Run Code Online (Sandbox Code Playgroud)
让我们创建一些数组:
Long[] l = { 1L, 2L, 3L };
Integer[] i = { 4, 5, 6 };
Double[] d = { 7., 8., 9. };
Run Code Online (Sandbox Code Playgroud)
我们的方法调用:
Number[] n …Run Code Online (Sandbox Code Playgroud) 在布置类层次结构时,我经常发现自己对能够封装功能同时共享代码之间的差距感到沮丧.当然,部分问题是缺少多重继承,但接口有所帮助.在我看来,无法在接口上定义受保护的方法是一个更大的问题.
标准解决方案似乎是拥有一个由受保护的抽象基类实现的公共接口.问题是我们有以下情况
public interface Foo {
public String getName();
}
abstract protected BaseFoo implements Foo {
abstract protected int getId();
private String name;
protected BaseFoo(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class ConcreteFoo extends BaseFoo {
public ConcreteFoo (String name) {
super(name);
}
@Override
protected int getId() {
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
}
// in the foo package with the …Run Code Online (Sandbox Code Playgroud)