在这篇文章中,我们将为您详细介绍写方法中的“预期类型'Union[str,bytearray]'取为'int'”警告的内容,并且讨论关于预期类型bool和找到的类型signaldi不匹配的相关问题。此
在这篇文章中,我们将为您详细介绍写方法中的“预期类型'Union [str,bytearray]'取为'int'”警告的内容,并且讨论关于预期类型bool和找到的类型signaldi不匹配的相关问题。此外,我们还会涉及一些关于actionscript-3 – 如何将bytearray转换为图像或图像到bytearray?、ByteArrayInputStream、ByteArrayInputStream与ByteArrayOutputStream、ByteArrayInputStream类的知识,以帮助您更全面地了解这个主题。
本文目录一览:- 写方法中的“预期类型'Union [str,bytearray]'取为'int'”警告(预期类型bool和找到的类型signaldi不匹配)
- actionscript-3 – 如何将bytearray转换为图像或图像到bytearray?
- ByteArrayInputStream
- ByteArrayInputStream与ByteArrayOutputStream
- ByteArrayInputStream类
写方法中的“预期类型'Union [str,bytearray]'取为'int'”警告(预期类型bool和找到的类型signaldi不匹配)
我的脚本使用预先生成的数据模式逐块写入文件:
# Data pattern generator def get_random_chunk_pattern(): return ''''.join(random.choice(ascii_uppercase + digits + ascii_lowercase) for _ in range(8))
....
# DedupChunk class CTOR:class DedupChunk: def __init__(self, chunk_size, chunk_pattern, chunk_position=0, state=DedupChunkStates.PENDING): self._chunk_size = chunk_size # chunk size in bytes self._chunk_pattern = chunk_pattern self._chunk_position = chunk_position self._state = state self.mapping = None @property def size(self): return self._chunk_size @property def pattern(self): return self._chunk_pattern @property def position(self): return self._chunk_position @property def state(self): return self._state
....
# Here Chunk object is being initialized (inside other class''s CTOR):chunk_size = random.randint(64, 192) * 1024 # in bytes while (position + chunk_size) < self.file_size: # generating random chunks number self.chunks.append(DedupChunk(chunk_size, DedupChunkPattern.get_random_chunk_pattern(), position))
....
# Actual writing with open(self.path, ''rb+'') as f: for chunk in self.chunks: f.write(chunk.pattern * (chunk.size // 8))
PyCharm``Expected type ''Union[str, bytearray]'' got ''int'' instead
在写入方法中显示警告
但是,在中删除 f.write(chunk.pattern * chunk.size)
或在外面进行除法时:
chunk.size //= 8f.write(chunk.pattern * chunk.size)
警告消失
这里到底发生了什么?
谢谢
答案1
小编典典忽略此警告。IDE会(根据有限的信息)对运行时的数据类型进行最佳猜测,但猜测是错误的。也就是说,如果您不知道某物实际上是什么,可以期望乘以某物int
会导致一个结果int
。
如果您真的想解决此问题,请chunk.pattern
通过为类编写文档字符串(或使用批注提供类型提示)来告诉IDE您期望什么。
例如。
class DedupChunk: """ :type _chunk_pattern: str ... other fields """ ... # rest of class
actionscript-3 – 如何将bytearray转换为图像或图像到bytearray?
我有面板控件,并希望从webservice加载图像作为backgroundimage.所以我使用setstyle()但不接受该图像.所以如何将该图像添加到我的面板背景图像中.请在此处告诉我您的想法.
解决方法
yourImage.source = yourByteArray;
问候!
ByteArrayInputStream
package com.tz.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 缓冲区会随着数据的不断写入而增长,存入数组,不涉及底层资源操作
* ByteArrayInputStream:在构造的时候,需要接收数据源,而且数据源是一个字节数组。
* ByteArrayOutputStream:在构造的时候,不用定义数据目的,该对象中已经内部封装了可变长度的字节数组,这就是数据目的地
* 因为这两个流对象都操作的数组,并没有使用系统资源,所以不用进行close关闭
* @author Administrator
* 流操作规律:
* 原设备 键盘,System.in硬盘,FileStream内存ArrayStream
* 目的设备 控制台 System.out 硬盘 fileStream,内存arraystream
*用流的读写思想来操作数组
*/
public class ByteArrayInputStreamDemo {
public static void main(String[] args) throws IOException {
//数据源
ByteArrayInputStream bis=new ByteArrayInputStream("".getBytes());
//数据目的
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int by=0;
while((by=bis.read())!=-1){
bos.write(by);
}
bos.writeTo(new OutputStream() {
@Override
public void write(int b) throws IOException {
}
});
}
}
ByteArrayInputStream与ByteArrayOutputStream
第一次看到ByteArrayOutputStream的时候是在Nutch的部分源码,后来在涉及IO操作时频频发现这两个类的踪迹,觉得确实是很好用,所以把它们的用法总结一下。
ByteArrayOutputStream的用法
以下是JDK中的记载:
public class ByteArrayOutputStream extends OutputStream
此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray()和 toString()获取数据。
关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何IOException。
我的个人理解是ByteArrayOutputStream是用来缓存数据的(数据写入的目标(output stream原义)),向它的内部缓冲区写入数据,缓冲区自动增长,当写入完成时可以从中提取数据。由于这个原因,ByteArrayOutputStream常用于存储数据以用于一次写入。
实例:
从文件中读取二进制数据,全部存储到ByteArrayOutputStream中。
FileInputStream fis=new FileInputStream("test");
BufferedInputStream bis=new BufferedInputStream(fis);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int c=bis.read();//读取bis流中的下一个字节
while(c!=-1){
baos.write(c);
c=bis.read();
}
bis.close();
byte retArr[]=baos.toByteArray();
ByteArrayInputStream的用法
相对而言,ByteArrayInputStream比较少见。先看JDK文档中的介绍:
public class ByteArrayInputStreamextends InputStreamByteArrayInputStream 包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪 read 方法要提供的下一个字节。
关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。
构造函数:
ByteArrayInputStream(byte[] buf)
注意它需要提供一个byte数组作为缓冲区。
与大部分Inputstream的语义类似,可以从它的缓冲区中读取数据,所以我们可以在它的外面包装另一层的inputstream以使用我们需要的读取方法。
个人认为一个比较好的用途是在网络中读取数据包,由于数据包一般是定长的,我们可以先分配一个够大的byte数组,比如byte buf[]=new byte[1024];
然后调用某个方法得到网络中的数据包,例如:
Socket s=...;
DataInputStream dis=new DataInputStream(s.getInputStream());
dis.read(buf);//把所有数据存到buf中
ByteArrayInputStream bais=new ByteArrayInputStream(buf); //把刚才的部分视为输入流
DataInputStream dis_2=new DataInputStream(bais);
//现在可以使用dis_2的各种read方法,读取指定的字节
比如第一个字节是版本号,dis_2.readByte();
等等……
上面的示例的两次包装看上去有点多此一举,但使用ByteArrayInputStream的好处是关掉流之后它的数据仍然存在。
ByteArrayInputStream类
一、说明
哈哈,这是学习Java之路的第一篇博文。虽然说接触学习Java有一段时间了,但是对流的概念一直并不是很清楚。也看了很多资料,但是感觉还是非常的抽象很难去理解。但是流又是Java中很重要的一部分,所以就决定花点时间好好弄一下。这篇文章也并非全部是我的原创文章,我浏览了写的比较好的博客,将其修改添加,加上一些自己的看法,就当成自己学习的笔记。仅供学习参考。参考原文链接
二、ByteArrayInputStream类
ByteArrayInputStream 是字节数组输入流。它继承于InputStream。
它包含一个内部缓冲区,该缓冲区包含从流中读取的字节;通俗点说,它的内部缓冲区就是一个字节数组,而ByteArrayInputStream本质就是通过字节数组来实现的。
我们都知道,InputStream通过read()向外提供接口,供它们来读取字节数据;而ByteArrayInputStream 的内部额外的定义了一个计数器,它被用来跟踪 read() 方法要读取的下一个字节。
InputStream类
InputStream类函数列表
函数 | 返回值 | 功能 |
public int read() throws IOEXception | 返回下一个数据字节 (返回 |
从输入流中读取数据的下一个字节 |
public int read(byte[] b) throws IOEXception | 以整数形式返回实际读取的字节数。 如果因为已经到达流末尾而不再有数据可用, 则返回 |
从输入流中读取一定数量的字节, 并将其存储在缓冲区数组 |
public int read(byte[] b,int off,int len) throws IOEXception |
读入缓冲区的总字节数; 如果因为已到达流末尾而不再有数据可用, 则返回 |
将输入流中最多 len 个数据字节读入 byte 数 |
public long skip(long n) throws IOEXception | 跳过的实际字节数 | 跳过和丢弃此输入流中数据的 n 个字 |
public int available() throws IOEXception |
可以不受阻塞地从此输入流读取 (或跳过)的估计字节数 |
返回此输入流下一个方法调用 可以不受阻塞地从此输入流读取 (或跳过)的估计字节数 |
public boolean markSupported() | 如果此输入流实例支持 mark 和 reset 方法, 则返回 |
测试此输入流是否支持 mark 和 reset 方法 |
public void mark(int readlimit) | 无 | 在此输入流中标记当前的位置 |
public void reset() throws IOEXception | 无 | 将此流重新定位到最后一次对此输入流 调用 |
public void close() throws IOEXception | 无 | 关闭此输入流并释放与该流关联的所有系统资源 |
关于上表的解释:
1.可以这样理解read()函数里的“读取下一个字节”,就是光标在前
2.mark()方法的参数在实现中没有什么实际的意义。
3.InputStream 的 close
方法不执行任何操作。
4.由于InputStream类是所以输入流的父类,方法的实现也就没有那么具体,它相当于给出了一个方法的规范,它的所以子类会按照这个规范去实现方法。也就是说,其子类方法的实现尽管不同,提供的功能却是相同的。
5.想了解更加详细的信息可以去查看jdk文档,是学习java很重要的工具。jdk文档
InputStream类源码分析
由于上述给出了此类函数比较详细的介绍,所以这里不作关于源码太多的说明。


1 package java.io;
2
3 public abstract class InputStream implements Closeable {
4
5 // 能skip的大小
6 private static final int MAX_SKIP_BUFFER_SIZE = 2048;
7
8 // 从输入流中读取数据的下一个字节。
9 public abstract int read() throws IOException;
10
11 // 将数据从输入流读入 byte 数组。
12 public int read(byte b[]) throws IOException {
13 return read(b, 0, b.length);
14 }
15
16 // 将最多 len 个数据字节从此输入流读入 byte 数组。
17 public int read(byte b[], int off, int len) throws IOException {
18 if (b == null) {
19 throw new NullPointerException();
20 } else if (off < 0 || len < 0 || len > b.length - off) {
21 throw new IndexOutOfBoundsException();
22 } else if (len == 0) {
23 return 0;
24 }
25
26 int c = read();
27 if (c == -1) {
28 return -1;
29 }
30 b[off] = (byte)c;
31
32 int i = 1;
33 try {
34 for (; i < len ; i++) {
35 c = read();
36 if (c == -1) {
37 break;
38 }
39 b[off + i] = (byte)c;
40 }
41 } catch (IOException ee) {
42 }
43 return i;
44 }
45
46 // 跳过输入流中的n个字节
47 public long skip(long n) throws IOException {
48
49 long remaining = n;
50 int nr;
51
52 if (n <= 0) {
53 return 0;
54 }
55
56 int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
57 byte[] skipBuffer = new byte[size];
58 while (remaining > 0) {
59 nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
60 if (nr < 0) {
61 break;
62 }
63 remaining -= nr;
64 }
65
66 return n - remaining;
67 }
68
69 public int available() throws IOException {
70 return 0;
71 }
72
73 public void close() throws IOException {}
74
75 public synchronized void mark(int readlimit) {}
76
77 public synchronized void reset() throws IOException {
78 throw new IOException("mark/reset not supported");
79 }
80
81 public boolean markSupported() {
82 return false;
83 }
84 }
说明:
1.Closeable 接口表示可以关闭的数据源或目标。这个接口只有一个方法close()。
ByteArrayInputStream类
如果还是觉得上面的内容理解起来还是很抽象,很难懂。没有关系,结合这个具体的InputStream类具体的子类ByteArrayInputStream类来看,会帮助你解开一部分困惑。计算机的学习就是这个样子,后面的知识需要前面的知识作为铺垫,前面的知识需要后面的知识才可以更深的理解。所以要前前后后的看一下,才可以真正的理解。
ByteArrayInputStream
包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪read
方法要提供的下一个字节。关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。
这是jdk文档中关于此类的描述。说明:
1.内部缓冲区指的是buf数组
2.内部计数器就是指字段pos,可以将其看成是光标
3.关闭 ByteArrayInputStream 无效指的是,关闭流的close()方法,在此类中时空方法
这里就不给出ByteArrayInputStream类的方法列表里,因为大体上和InputStream类方法列表的描述是一样的,只是方法的实现是不同的而已。
ByteArrayInputStream类源码分析
源码还是很有必要看一下的可以加强对此类的理解


1 public class ByteArrayInputStream extends InputStream {
2
3 // 保存字节输入流数据的字节数组
4 protected byte buf[];
5
6 // 下一个会被读取的字节的索引
7 protected int pos;
8
9 // 用于标记输入流当前位置,就是标记pos
10 protected int mark = 0;
11
12 // 字节流的长度
13 protected int count;
14
15 // 构造函数:创建一个内容为buf的字节流
16 public ByteArrayInputStream(byte buf[]) {
17 // 初始化“字节流对应的字节数组为buf”
18 this.buf = buf;
19 // 初始化“下一个要被读取的字节索引号为0”
20 this.pos = 0;
21 // 初始化“字节流的长度为buf的长度”
22 this.count = buf.length;
23 }
24
25 // 构造函数:创建一个内容为buf的字节流,并且是从offset开始读取数据,读取的长度最多为length
26 public ByteArrayInputStream(byte buf[], int offset, int length) {
27 // 初始化“字节流对应的字节数组为buf”
28 this.buf = buf;
29 // 初始化“下一个要被读取的字节索引号为offset”
30 this.pos = offset;
31 // 初始化“字节流的长度”
32 this.count = Math.min(offset + length, buf.length);
33 // 初始化“标记的字节流读取位置”
34 this.mark = offset;
35 }
36
37 // 读取下一个字节
38 public synchronized int read() {
39 return (pos < count) ? (buf[pos++] & 0xff) : -1;
40 }
41
42 // 将“字节流的数据写入到字节数组b中”
43 // off是“字节数组b的偏移地址”,表示从数组b的off开始写入数据
44 // len是“读到b数组中的字节最大长度”
45 public synchronized int read(byte b[], int off, int len) {
46 if (b == null) {
47 throw new NullPointerException();
48 } else if (off < 0 || len < 0 || len > b.length - off) {
49 throw new IndexOutOfBoundsException();
50 }
51
52 if (pos >= count) {
53 return -1;
54 }
55
56 int avail = count - pos;
57 if (len > avail) {
58 len = avail;
59 }
60 if (len <= 0) {
61 return 0;
62 }
63 System.arraycopy(buf, pos, b, off, len);
64 pos += len;
65 return len;//从字节流中成功读出的字节数
66 }
67
68 // 跳过“字节流”中的n个字节。
69 public synchronized long skip(long n) {
70 long k = count - pos;
71 if (n < k) {
72 k = n < 0 ? 0 : n;
73 }
74
75 pos += k;
76 return k;//成功跳过的字节数
77 }
78
79 // “能否读取字节流的下一个字节”
80 public synchronized int available() {
81 return count - pos;
82 }
83
84 // 是否支持“标签”
85 public boolean markSupported() {
86 return true;
87 }
88
89 // 保存当前位置。readAheadLimit在此处没有任何实际意义
90 public void mark(int readAheadLimit) {
91 mark = pos;
92 }
93
94 // 重置“字节流的读取索引”为“mark所标记的位置”
95 public synchronized void reset() {
96 pos = mark;
97 }
98
99 public void close() throws IOException {
100 }
101 }
真的是排版能力有限了。此博文仅作学习之用,欢迎指正。
关于写方法中的“预期类型'Union [str,bytearray]'取为'int'”警告和预期类型bool和找到的类型signaldi不匹配的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于actionscript-3 – 如何将bytearray转换为图像或图像到bytearray?、ByteArrayInputStream、ByteArrayInputStream与ByteArrayOutputStream、ByteArrayInputStream类的相关信息,请在本站寻找。
本文标签: