BufferOverflowException 的目的是什么
what is the purpose of BufferOverflowException
我不太明白meaning/purpose BufferOverflowException();
在我的课程中,我们使用它编写队列代码,并在向队列添加元素时使用 BufferOverflowException。
根据docs.oracle,它的意思是"Unchecked exception thrown when a relative put operation reaches the target buffer's limit.",但我还是不明白它的意思。
public class FIFOQueue<T>{
T[] data;
int first=0;
int last=0;
boolean full = false;
public FIFOQueue(int capacity){
data = (T[]) new Object[capacity];
}
public void add(T element){
if (full)
throw new BufferOverflowException();
data[last] = element;
last++;
if (last == data.length)
last = 0;
if (last == first)
full = true;
}
缓冲区溢出意味着向应用程序提供了太多数据。
示例:将一本书的文本复制到 phone 上的 'New Contact Name'。
通常,如果处理不当,这会导致 崩溃...
更重要的是,它可能是一个 安全漏洞 !
额外的数据可以存储在程序指定内存之外,额外的数据可以是可执行代码。
因此,始终验证用户输入是一种很好的做法! :)
我不太明白meaning/purpose BufferOverflowException(); 在我的课程中,我们使用它编写队列代码,并在向队列添加元素时使用 BufferOverflowException。
根据docs.oracle,它的意思是"Unchecked exception thrown when a relative put operation reaches the target buffer's limit.",但我还是不明白它的意思。
public class FIFOQueue<T>{
T[] data;
int first=0;
int last=0;
boolean full = false;
public FIFOQueue(int capacity){
data = (T[]) new Object[capacity];
}
public void add(T element){
if (full)
throw new BufferOverflowException();
data[last] = element;
last++;
if (last == data.length)
last = 0;
if (last == first)
full = true;
}
缓冲区溢出意味着向应用程序提供了太多数据。
示例:将一本书的文本复制到 phone 上的 'New Contact Name'。
通常,如果处理不当,这会导致 崩溃...
更重要的是,它可能是一个 安全漏洞 !
额外的数据可以存储在程序指定内存之外,额外的数据可以是可执行代码。
因此,始终验证用户输入是一种很好的做法! :)