]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.compressions/src/org/simantics/compressions/impl/Buffers.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.compressions / src / org / simantics / compressions / impl / Buffers.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 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.compressions.impl;
13
14 import java.nio.ByteBuffer;
15
16 /**
17  * @author Tuukka Lehtonen
18  */
19 public class Buffers {
20
21         public static byte[] getInputArray(ByteBuffer buffer) {
22                 if (buffer.hasArray())
23                         return buffer.array();
24                 // Poor performance fallback involving copying.
25                 byte[] copy = new byte[buffer.remaining()];
26                 buffer.get(copy);
27                 return copy;
28         }
29
30         public static byte[] getOutputArray(ByteBuffer buffer) {
31                 return buffer.hasArray() ? buffer.array() : new byte[buffer.remaining()];
32         }
33
34         public static void writeOutput(ByteBuffer buffer, byte[] array) {
35                 if (buffer.hasArray() && buffer.array() == array)
36                         return;
37                 // Poor performance fallback involving copying.
38                 int pos = buffer.position();
39                 buffer.put(array);
40                 buffer.position(pos);
41         }
42
43 }