Answer the question
In order to leave comments, you need to log in
How to understand the expression when writing values to the output stream?
Why is this expression used in the writePoint() method? How then, when reading, get back the value of x and y, if only one value is written?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());
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question