-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathTask01Main.java
More file actions
27 lines (22 loc) · 1.06 KB
/
Task01Main.java
File metadata and controls
27 lines (22 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.task01;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Task01Main {
public static void main(String[] args) throws IOException {
//здесь вы можете вручную протестировать ваше решение, вызывая реализуемый метод и смотря результат
// например вот так:
System.out.println(checkSumOfStream(new ByteArrayInputStream(new byte[]{0x33, 0x45, 0x01})));
}
public static int checkSumOfStream(InputStream inputStream) throws IOException {
if (inputStream == null)
throw new IllegalArgumentException("Передача пустого аргумента");
int sum = 0;
int currentValue;
while ((currentValue = inputStream.read()) > -1) {
sum = Integer.rotateLeft(sum, 1) ^ currentValue;
// сдвиг на 1 бит влево исключающее ИЛИ
}
return sum;
}
}