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