2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.util;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.Arrays;
25 public class CommitOutputStream extends OutputStream
27 private final OutputStream m_sink;
28 private byte m_buffer[];
31 public CommitOutputStream(OutputStream sink)
34 throw new NullPointerException("sink == null");
37 m_buffer = new byte[1024];
41 public void write(int data)
43 if (m_size >= m_buffer.length)
44 m_buffer = Arrays.copyOf(m_buffer, 2 * m_buffer.length);
46 m_buffer[m_size++] = (byte) data;
50 public void write(byte buffer[], int offset, int length)
53 throw new IndexOutOfBoundsException("offset < 0 : " + offset);
56 throw new IndexOutOfBoundsException("length < 0 : " + length);
58 if (offset + length > buffer.length)
59 throw new IndexOutOfBoundsException("offset + length > buffer.length : " + offset + " + " + length + " > " + buffer.length);
64 if (m_size + length > m_buffer.length)
65 m_buffer = Arrays.copyOf(m_buffer, Math.max(m_size + length, 2 * m_buffer.length));
67 System.arraycopy(buffer, offset, m_buffer, m_size, length);
77 public void commit() throws IOException
82 m_sink.write(m_buffer, 0, m_size);
87 public void rollback()
93 public void flush() throws IOException
99 public void close() throws IOException