我们假设我有以下内容:
final ByteArrayOutputStream boas = new ByteArrayOutputStream();
final byte[] sample = {1,2,3,4,5,6,7,8,9,10};
boas.write(sample);
Run Code Online (Sandbox Code Playgroud)
此时,byte buf[]内部的后备数组boas包含上面的10个字节(和22个填充字节).
如果我boas在count和上调用reset ,size并且所有其他因素都表明它是空的,但内部byte buf[]保持不变并填充.
无论如何要真正清除这一点而不创造一个全新的ByteArrayOutputStream?
更一般地说,ByteArrayOutputStream是否有这样的行为而不是清空byte[]?
我目前有以下简单的控制器:
class SimpleController < ApplicationController
def index
@results = fetch_results
end
end
Run Code Online (Sandbox Code Playgroud)
fetch_results是一个相当昂贵的操作,所以虽然上述工作,我不想每次刷新页面时运行它.如何解除更新,@results以便按固定的时间表更新,让我们说每15分钟更新一次.
这样,每次页面加载时,它都会返回当前@results值,最坏的情况是过时14分59秒.
我正在使用google gauva版本11.0.1并拥有这段代码:
ImmutableList.copyOf(items);
Run Code Online (Sandbox Code Playgroud)
其中items是ConcurrentLinkedQueue.我偶尔会看到这个错误:
java.lang.ArrayIndexOutOfBoundsException: 10
at java.util.AbstractCollection.toArray(AbstractCollection.java:126)
at com.google.common.collect.ImmutableList.copyFromCollection(ImmutableList.java:278)
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:247)
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:217)
Run Code Online (Sandbox Code Playgroud)
鉴于问题完全在番石榴库中,有谁知道为什么?
根据以下正确答案进行更新
感谢wolfcastle的帮助,我设法在我的应用程序之外单独重现了这个问题.
final int itemsToPut = 30000;
final ConcurrentLinkedQueue<Integer> items = new ConcurrentLinkedQueue<Integer>();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < itemsToPut; i++) {
items.add(i);
}
}
}, "putter-thread").start();
final Iterable<String> transformed = Collections2.transform(items, new Function<Integer, String>() {
public String apply(Integer integer) {
return "foo-" + integer;
}
});
ImmutableList.copyOf(transformed);
Run Code Online (Sandbox Code Playgroud)
每次运行它会产生以下结果:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 21480
at …Run Code Online (Sandbox Code Playgroud) 我最近看到了有关二进制编码的讨论,给出的示例Date是以两个字节存储Java 对象的日期部分(日,月和年)。我现在正试图从演讲中了解代码片段:
long time = new Date().getTime(); // time in ms since epoch
time /= 86400000; // ms in a day
byte a = (byte)(time >>> 8);
byte b = (byte)(time);
Run Code Online (Sandbox Code Playgroud)
现在,我缺少的是将这两个字节转换回原始日期的日,月和年的方式似乎很“简单”。我也不确定如果我们同时保留原始时间值作为字节,为什么还要使用两个字节。
有人可以解释一下这怎么可能吗?我了解上面的代码在做什么,只是不知道如何还原原始日期。
更新资料
这是谈话,有问题的幻灯片是20/21
http://www.slideshare.net/jtdavies/turn-your-xml-into-binary-java-one-2014
我有两个Date对象,例如:
first = Fri, 02 Dec 2016
last = Wed, 01 Mar 2017
Run Code Online (Sandbox Code Playgroud)
在它们之间获得独特的月份和年份数组的最有效方法是什么?在这种情况下,我追求的是:
Dec 2016
Jan 2017
Feb 2017
Mar 2017
Run Code Online (Sandbox Code Playgroud) 在Teamcity(版本7.1)中,如何设置要触发的构建,例如每20分钟运行一次?
我注意到你可以设置基于时间的计划,例如"每天18:00运行这个版本",但这不是我想要的.
鉴于我们对浮点精度的了解,为什么这个代码:
float a = 0.1f;
System.out.println(a);
Run Code Online (Sandbox Code Playgroud)
打印0.1而不是0.100000001?