]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/clipboard/streams/RemoveBitmapHeaderOutputStream.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / clipboard / streams / RemoveBitmapHeaderOutputStream.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.ui.gfx.clipboard.streams;
13
14 import java.io.IOException;
15 import java.io.OutputStream;
16
17 import org.simantics.utils.ui.gfx.clipboard.headers.BitmapFileHeader;
18
19
20 /**
21  * Removes WIN32 API BITMAPFILEHEADER from the beginning of stream
22  * 
23  * @author Marko Luukkainen
24  *
25  */
26 public class RemoveBitmapHeaderOutputStream extends OutputStream{
27     private OutputStream oStream;
28     private int byteCount = 0;
29     
30     public RemoveBitmapHeaderOutputStream(OutputStream oStream) {
31         this.oStream = oStream;
32     }
33     
34     @Override
35     public void write(byte[] b) throws IOException {
36         if (byteCount >= BitmapFileHeader.sizeof) {
37             super.write(b);
38         } else if (b.length >= BitmapFileHeader.sizeof - byteCount){
39             byte[] newArray = new byte[b.length - BitmapFileHeader.sizeof + byteCount];
40             System.arraycopy(b, BitmapFileHeader.sizeof - byteCount, newArray, 0, newArray.length);
41             byteCount = BitmapFileHeader.sizeof;
42             super.write(newArray);    
43         } else {
44             byteCount+= b.length;
45         }
46         super.write(b);
47     }
48     
49     @Override
50     public void write(byte[] b, int off, int len) throws IOException {
51         if (byteCount >= BitmapFileHeader.sizeof)
52             super.write(b, off, len);
53         else
54             throw new IOException("Operation not supported, cannot remove header"); 
55     }
56     
57     @Override
58     public void write(int b) throws IOException {
59         if (byteCount >= BitmapFileHeader.sizeof) {
60             oStream.write(b);
61         } else {
62             byteCount++;
63         }
64     }
65 }