我试图读取一个二进制文件,它是以某种模式编写的,例如:string, string, byte
我上网浏览了一下,发现了这段代码:
while (br.BaseStream.Position<br.BaseStream.Length)
{
br.ReadString();
br.ReadString();
br.ReadByte();
}
Run Code Online (Sandbox Code Playgroud)
尽管这是简单的代码,但我无法理解底层流(BaseStream)的含义是什么?有人可以给我一个简短的解释吗?
我在互联网上有一个文件的网址。我需要计算 SHA1 哈希值,并逐行读取该文件。我知道如何做到这一点,但我读了这个文件两次,这可能不是一个很好的解决方案。
我怎样才能更有效地做到这一点?
这是我的代码:
URL url = new URL(url);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(1000);
urlConnection.setReadTimeout(1000);
logger.error(urlConnection.getContent() + " ");
InputStream is = urlConnection.getInputStream();
// first reading of file is:
int i;
File file = new File("nameOfFile");
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(file.getName()));
while ((i = bis.read()) != -1) {
bos.write(i);
}
bos.flush();
bis.close();
sha1(file);
// second reading of file is:
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) …Run Code Online (Sandbox Code Playgroud) 我正在将传统的 Spring 应用程序切换到 Spring Boot。
使用 spring cloud stream rabbitmq 迁移 rabbitmq 代码存在问题。
在遗留系统中,rabbitmq 队列是通过提供交换、路由密钥和队列名称来设置的。
例如,
exchange name = mq-test.topic
routingKey = mq-test
queueName = aa.mq-test
Run Code Online (Sandbox Code Playgroud)
所以在rabbitmq 管理视图中我可以看到exchange 是mq-test.topic,queue 是aa.mq-test。
但是对于 Spring Cloud 流,队列名称点缀着目的地,例如
mq-test.topic.aa.mq-test
我的 spring 云流属性是这样的。
spring.cloud.stream.bindings.channelName.destination=mq-test.topic
spring.cloud.stream.bindings.channelName.producer.bindingRoutingKey=mq-test
spring.cloud.stream.bindings.channelName.producer.requiredGroups=aa.mq-test
Run Code Online (Sandbox Code Playgroud)
我还代表 bindingRoutingKey 使用了 routingKeyExpression 属性,但结果是一样的。
有遗留应用程序通过队列名称使用数据,而我的新应用程序仅生成数据,因此我无法更改交换和队列名称策略。
如何使用 Spring Cloud 流保持交换/队列命名?
任何帮助表示赞赏。
我想要一个包含来自 Stream 的项目的 ListView。当然,List 的内容应该反映 Stream 中的变化。由于我的设计师怪癖,我希望列表中的项目由分隔符分隔。
只是想知道,使用分隔符创建 ListView 并对 Stream 更改做出反应的正确方法是什么。
body: StreamBuilder(
stream: someStream,
builder: (ctx, snapshot) {
return ListView.separated(
separatorBuilder: (context, index) => Divider(color: Colors.black),
itemCount: ***whatsHere***?,
itemBuilder: (BuildContext ctx, int index) {
...
Run Code Online (Sandbox Code Playgroud)
希望我错过了什么。由于以某种方式获取源流长度的想法至少看起来很奇怪,因为流的异步性质。StatefullWidget 使用流订阅(和 setState 调用)似乎是可行的,但是 StreamBuilder 的发明目的完全相同,不是吗?
我有一个具有层次关系的 firestore 数据库。“父”文档具有子对象的集合(“子”)。我想收到一系列家长反对意见。因此,如果 Fire 存储中的父级更改,则流应提供一个新的 Parent 对象,其中所有子级都已加载。
下面的代码是我想要实现的。问题出在标记为“====> 问题”的行中,该行插入到代码中。
编译器说:返回类型“列表>”不是匿名closure.dart(return_of_invalid_type_from_closure)定义的“列表”。
我不知道如何将此流转换为对异步调用的“映射”调用以获取列表而不是未来
有人知道 Flutter/Dart 中分层 Firestore 数据的好样本吗?
import 'package:cloud_firestore/cloud_firestore.dart';
final parentsCollection = Firestore.instance.collection('parents');
Stream<List<Parent>> jots() {
return parentsCollection.snapshots().map((snapshot) {
//====================================================
return snapshot.documents // <===== Problem
//====================================================
.map((doc) async {
var parentEntity = await ParentEntity.fromSnapshot(doc);
return Parent.fromEntity(parentEntity);
}).toList();
});
}
class Parent {
final String id;
final String data;
final List<Child> children ;
Parent(this.data, {
String id,
List<Child> children
})
: this.id = id ?? '',
this.children = children ?? List<Child>()
; …Run Code Online (Sandbox Code Playgroud) 这是导致问题的 Streambuilder 的 builder 方法中的代码:
List<User> users = snapshot.data;
users.sort((user1, user2) => (user1.distanceInKm ?? 1000).compareTo(user2.distanceInKm ?? 1000));
Run Code Online (Sandbox Code Playgroud)
如果我为 Streambuilder 使用以下流,则上述排序有效:
static Stream<List<User>> getUsersStreamWithDistance(
{@required User loggedInUser}) {
try {
var userSnapshots = _fireStore.collection('users').snapshots().map(
(snap) => snap.documents
.map((doc) => User.fromMap(map: doc.data))
.where((user) => user.email != loggedInUser.email)
.map((user) {
user.updateDistanceToOtherUser(otherUser: loggedInUser);
return user;
}).toList());
return userSnapshots;
} catch (e) {
print(e);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用以下流时,这是我需要的(ZipStream 来自 rxdart 包):
static Stream<List<User>> getSpecifiedUsersStreamWithDistance(
{@required User loggedInUser, @required List<String> uids}) {
try {
List<Stream<User>> …Run Code Online (Sandbox Code Playgroud) 我正在学习流并从教科书中复制了以下应用程序。当我的朋友在他的 Windows 机器上编译并运行时,它运行良好。当我在 Ubuntu 18.04 机器上运行应用程序时,输入工作正常,但这些值似乎对应用程序没有任何影响,即输入0不会导致程序退出。我的输出在代码下方。
在不同的机器上编译时什么会导致不同的行为,为什么这在我的机器上不起作用?
int main(int argc, char* argv[])
{
文件 *fpt;
字符字节;
long int where,move;
如果(argc != 2)
{
printf("用法:fileseek 文件名\n");
返回(0);
}
fpt = fopen(argv[1], "r");
如果(fpt == NULL)
{
printf("无法打开文件 %s 进行读取\n", argv[1]);
返回(0);
}
同时(1)
{
where = ftell(fpt);
fread(&byte,1,1,fpt);
fseek(fpt,-1,SEEK_CUR);
printf("Byte %d: %d (%c)\n", where, byte, byte);
printf("输入#bytes (+ or -) 移动,或 0 退出:");
scanf("%d", &move);
printf("移动:%d\n",移动);
如果(移动== 0)
休息;
fseek(fpt,move,SEEK_CUR);
}
fclose(fpt);
}
输出
jonathon@dev1:~/hoover/ch5/build$ ./fileseek text.txt
Byte 0: …Run Code Online (Sandbox Code Playgroud) 在 Delphi 10.4 中,我尝试使用以下代码将 a 转换TStream为 a string:
function MyStreamToString(aStream: TStream): string;
var
SS: TStringStream;
begin
if aStream <> nil then
begin
SS := TStringStream.Create('');
try
SS.CopyFrom(aStream, 0); // Exception: TStream.Seek not implemented
Result := SS.DataString;
finally
SS.Free;
end;
end else
begin
Result := '';
end;
end;
Run Code Online (Sandbox Code Playgroud)
但在此代码行中,我收到一个异常“TStream.Seek 未实现”: SS.CopyFrom(aStream, 0);
为什么?我怎样才能“治愈”这个代码?
我有一个Set<String> set1and Set<String> set2,以及 2 个函数getSet1ElementScore(String s)and getSet2ElementScore(String s)(返回整数),并且想要将两个集合中的所有元素作为其键插入到 HashMap 中,每个键的值要么根据键来自哪个集合计算,getSet1ElementScore要么getSet2ElementScore取决于键来自哪个集合。
我可以使用流来进行管道传输吗?
我一直使用 Common Lisptime宏来检查实现的速度。但是,我希望能够将计时输出写入流(文件),以便我可以对其进行一些统计分析或以某种方式对其进行过滤。
到目前为止,我还没有找到任何使我能够做到这一点的命令或库。我将不胜感激这方面的帮助。