Fixes null captured parameters
[jpf-core.git] / src / main / gov / nasa / jpf / util / CommitOutputStream.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18
19 package gov.nasa.jpf.util;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.Arrays;
24
25 public class CommitOutputStream extends OutputStream
26 {
27    private final OutputStream m_sink;
28    private       byte         m_buffer[];
29    private       int          m_size;
30    
31    public CommitOutputStream(OutputStream sink)
32    {
33       if (sink == null)
34          throw new NullPointerException("sink == null");
35       
36       m_sink   = sink;
37       m_buffer = new byte[1024];
38    }
39    
40    @Override
41   public void write(int data)
42    {
43       if (m_size >= m_buffer.length)
44          m_buffer = Arrays.copyOf(m_buffer, 2 * m_buffer.length);
45       
46       m_buffer[m_size++] = (byte) data;
47    }
48
49    @Override
50   public void write(byte buffer[], int offset, int length)
51    {
52       if (offset < 0)
53          throw new IndexOutOfBoundsException("offset < 0 : " + offset);
54       
55       if (length < 0)
56          throw new IndexOutOfBoundsException("length < 0 : " + length);
57          
58            if (offset + length > buffer.length)
59               throw new IndexOutOfBoundsException("offset + length > buffer.length : " + offset + " + " + length + " > " + buffer.length);
60       
61       if (length == 0)
62          return;
63       
64       if (m_size + length > m_buffer.length)
65          m_buffer = Arrays.copyOf(m_buffer, Math.max(m_size + length, 2 * m_buffer.length));
66       
67       System.arraycopy(buffer, offset, m_buffer, m_size, length);
68       
69       m_size += length;
70    }
71    
72    public int getSize()
73    {
74       return(m_size);
75    }
76    
77    public void commit() throws IOException
78    {
79       if (m_size == 0)
80          return;
81       
82       m_sink.write(m_buffer, 0, m_size);
83       
84       m_size = 0;
85    }
86    
87    public void rollback()
88    {
89       m_size = 0;
90    }
91    
92    @Override
93   public void flush() throws IOException
94    {
95       m_sink.flush();      
96    }
97    
98    @Override
99   public void close() throws IOException
100    {
101       m_sink.close();
102    }
103 }