Для чего в методе writePoint() используется такое выражение? Как потом при считывании получить обратно значение x и y, если записывается всего одно значение value?
int value = point.getX() << 4 | point.getY();
class Point {
private final int x;
private final int y;
public Point(int x, int y) {
if (x < 0 || 15 < x) {
throw new IllegalArgumentException();
}
if (y < 0 || 15 < y) {
throw new IllegalArgumentException();
}
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
class EntityOutputStream implements EntityOutput {
private final DataOutput out;
public EntityOutputStream(OutputStream out) {
this.out = new DataOutputStream(out);
}
@Override
public void writePoint(Point point) throws IOException {
int value = point.getX() << 4 | point.getY();
out.writeByte(value);
}
}
public class App {
public static void main(String[] args) throws IOException {
Point point = new Point(11, 14);
EntityOutputStream out = new EntityOutputStream(new FileOutputStream("c://file.txt"));
EntityInput in = new EntityInputStream(new FileInputStream("c://file.txt"));
out.writePoint(point);
System.out.println(in.readPoint());
}
}