--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui.gfx.clipboard.streams;\r
+\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+\r
+import org.simantics.utils.ui.gfx.clipboard.headers.BitmapFileHeader;\r
+\r
+\r
+/**\r
+ * Removes WIN32 API BITMAPFILEHEADER from the beginning of stream\r
+ * \r
+ * @author Marko Luukkainen\r
+ *\r
+ */\r
+public class RemoveBitmapHeaderOutputStream extends OutputStream{\r
+ private OutputStream oStream;\r
+ private int byteCount = 0;\r
+ \r
+ public RemoveBitmapHeaderOutputStream(OutputStream oStream) {\r
+ this.oStream = oStream;\r
+ }\r
+ \r
+ @Override\r
+ public void write(byte[] b) throws IOException {\r
+ if (byteCount >= BitmapFileHeader.sizeof) {\r
+ super.write(b);\r
+ } else if (b.length >= BitmapFileHeader.sizeof - byteCount){\r
+ byte[] newArray = new byte[b.length - BitmapFileHeader.sizeof + byteCount];\r
+ System.arraycopy(b, BitmapFileHeader.sizeof - byteCount, newArray, 0, newArray.length);\r
+ byteCount = BitmapFileHeader.sizeof;\r
+ super.write(newArray); \r
+ } else {\r
+ byteCount+= b.length;\r
+ }\r
+ super.write(b);\r
+ }\r
+ \r
+ @Override\r
+ public void write(byte[] b, int off, int len) throws IOException {\r
+ if (byteCount >= BitmapFileHeader.sizeof)\r
+ super.write(b, off, len);\r
+ else\r
+ throw new IOException("Operation not supported, cannot remove header"); \r
+ }\r
+ \r
+ @Override\r
+ public void write(int b) throws IOException {\r
+ if (byteCount >= BitmapFileHeader.sizeof) {\r
+ oStream.write(b);\r
+ } else {\r
+ byteCount++;\r
+ }\r
+ }\r
+}\r