小编voo*_*voo的帖子

如何检查InputStream是否被Gzip化?

有没有办法检查InputStream是否已被gzip压缩?这是代码:

public static InputStream decompressStream(InputStream input) {
    try {
        GZIPInputStream gs = new GZIPInputStream(input);
        return gs;
    } catch (IOException e) {
        logger.info("Input stream not in the GZIP format, using standard format");
        return input;
    }
}
Run Code Online (Sandbox Code Playgroud)

我试过这种方式,但它没有按预期工作 - 从流中读取的值无效.编辑:添加了我用来压缩数据的方法:

public static byte[] compress(byte[] content) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        GZIPOutputStream gs = new GZIPOutputStream(baos);
        gs.write(content);
        gs.close();
    } catch (IOException e) {
        logger.error("Fatal error occured while compressing data");
        throw new RuntimeException(e);
    }
    double ratio = (1.0f * content.length / …
Run Code Online (Sandbox Code Playgroud)

java gzip http inputstream httpurlconnection

51
推荐指数
6
解决办法
4万
查看次数

在numpy中将一个数组索引为另一个数组

假设我有一个带有一些任意值的矩阵A:

array([[ 2, 4, 5, 3],
       [ 1, 6, 8, 9],
       [ 8, 7, 0, 2]])
Run Code Online (Sandbox Code Playgroud)

并且矩阵B包含A中元素的索引:

array([[0, 0, 1, 2],
       [0, 3, 2, 1],
       [3, 2, 1, 0]])
Run Code Online (Sandbox Code Playgroud)

我该如何选择值一个被指出,即:

A[B] = [[2, 2, 4, 5],
        [1, 9, 8, 6],
        [2, 0, 7, 8]]
Run Code Online (Sandbox Code Playgroud)

python numpy

17
推荐指数
3
解决办法
2250
查看次数

使用 ExecutorService 将任务确定性分配给线程

给定 Executor 服务具有固定的线程池,是否可以保证任务到线程的确定性分配?更准确地说,假设只有两个线程,即 pool-thread-0 和 pool-thread-1,并且有 2 个要执行的任务的集合。我希望实现的是前一个线程始终执行第一个线程,而后者处理剩余的线程。

这是一个例子:

public static void main(String[] args) throws InterruptedException, ExecutionException {
    ExecutorService executorService = newFixedThreadPool(2,
            new ThreadFactoryBuilder().setNameFormat("pool-thread-%d").build());

    for (int i = 0; i < 5; i++) {
        List<Callable<Integer>> callables = ImmutableList.of(createCallable(1), createCallable(2));
        executorService.invokeAll(callables);

    }
}

public static Callable<Integer> createCallable(final int task) {
    return new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {
            currentThread().sleep(1000);
            System.out.println(Thread.currentThread().getName() + " executes task num: " + task);
            return task;
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

我的机器的示例输出:

pool-thread-0 executes task num: …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading executorservice

5
推荐指数
1
解决办法
2465
查看次数

切片外numpy数组

给定2d矩阵,例如:

A = array([[ 0,  1,  2,  3],
          [ 4,  5,  6,  7],
          [ 8,  9, 10, 11],
          [12, 13, 14, 15]])
Run Code Online (Sandbox Code Playgroud)

如何NxN在给定元素周围选择窗口,以便如果窗口超出原始数组,则窗口将填充任意(例如平均值)值?

例:

neighbors(A, x=0, y=0, N=3)
Run Code Online (Sandbox Code Playgroud)

会屈服

array([[ 2.5,  2.5,  2.5],
       [ 2.5,    0,    1],
       [ 2.5,    4,    5]])
Run Code Online (Sandbox Code Playgroud)

python numpy

4
推荐指数
1
解决办法
612
查看次数