1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.exif;
18
19import java.io.FilterOutputStream;
20import java.io.IOException;
21import java.io.OutputStream;
22import java.nio.ByteBuffer;
23import java.nio.ByteOrder;
24
25class OrderedDataOutputStream extends FilterOutputStream {
26    private final ByteBuffer mByteBuffer = ByteBuffer.allocate(4);
27
28    public OrderedDataOutputStream(OutputStream out) {
29        super(out);
30    }
31
32    public void setByteOrder(ByteOrder order) {
33        mByteBuffer.order(order);
34    }
35
36    public void writeShort(short value) throws IOException {
37        mByteBuffer.rewind();
38        mByteBuffer.putShort(value);
39        out.write(mByteBuffer.array(), 0, 2);
40     }
41
42    public void writeInt(int value) throws IOException {
43        mByteBuffer.rewind();
44        mByteBuffer.putInt(value);
45        out.write(mByteBuffer.array());
46    }
47
48    public void writeRational(Rational rational) throws IOException {
49        writeInt((int) rational.getNominator());
50        writeInt((int) rational.getDenominator());
51    }
52}
53