ucar.nc2.iosp.netcdf3.N3nioCD.save Maven / Gradle / Ivy
/*
* Copyright 1998-2009 University Corporation for Atmospheric Research/Unidata
*
* Portions of this software were developed by the Unidata Program at the
* University Corporation for Atmospheric Research.
*
* Access and use of this software shall impose the following obligations
* and understandings on the user. The user is granted the right, without
* any fee or cost, to use, copy, modify, alter, enhance and distribute
* this software, and any derivative works thereof, and its supporting
* documentation for any purpose whatsoever, provided that this entire
* notice appears in all copies of the software, derivative works and
* supporting documentation. Further, UCAR requests that the user credit
* UCAR/Unidata in any publications that result from the use of this
* software or in any product that includes this software. The names UCAR
* and/or Unidata, however, may not be used in any advertising or publicity
* to endorse or promote any products or commercial entity unless specific
* written permission is obtained from UCAR/Unidata. The user also
* understands that UCAR/Unidata is not obligated to provide the user with
* any support, consulting, training or assistance of any kind with regard
* to the use, operation and performance of this software nor to provide
* the user with any updates, revisions, new versions or "bug fixes."
*
* THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package ucar.nc2.iosp.netcdf3;
import ucar.ma2.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.URL;
import java.util.*;
/**
* use direct channels for bulk reading and writing.
*/
class N3nioCD extends N3iosp {
protected FileChannel channel;
public void open(String filename, ucar.nc2.NetcdfFile ncfile, boolean readonly) throws IOException {
super.open( filename, ncfile, readonly);
channel = raf.getChannel();
if (debug) out.println ("Opened file to read:'" + filename+ "', size=" + channel.size());
}
/**
* Read data subset from file for a variable, create primitive array.
* @param beginOffset: variable's beginning byte offset in file.
* @param index handles skipping around in the file.
* @param source from this buye buffer
* @param dataType dataType of the variable
* @return primitive array with data read in
*/
protected Object readData( int beginOffset, Indexer index, DataType dataType) throws IOException {
int offset = 0;
int chunk = index.getChunkSize();
int size = index.getTotalSize();
if ((dataType == DataType.BYTE) || (dataType == DataType.CHAR)) {
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk);
byte[] pa = new byte[size];
while (index.hasNext()) {
bbuff.clear();
channel.read(bbuff, (long) beginOffset + index.next());
bbuff.flip();
bbuff.get( pa, offset, chunk); // copy into primitive array
offset += chunk;
}
return (dataType == DataType.BYTE) ? pa : (Object) convertByteToChar( pa);
} else if (dataType == DataType.SHORT) {
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*2);
ShortBuffer tbuff = bbuff.asShortBuffer(); // typed buffer
short[] pa = new short[size];
while (index.hasNext()) {
bbuff.clear();
channel.read(bbuff, (long) beginOffset + index.next());
tbuff.position(0).limit(chunk);
tbuff.get(pa, offset, chunk); // copy from typed buffer into primitive array
offset += chunk;
}
return pa;
} else if (dataType == DataType.INT) {
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*4);
IntBuffer tbuff = bbuff.asIntBuffer(); // typed buffer
int[] pa = new int[size];
while (index.hasNext()) {
bbuff.clear();
channel.read(bbuff, (long) beginOffset + index.next());
tbuff.position(0).limit(chunk);
tbuff.get(pa, offset, chunk); // copy from typed buffer into primitive array
offset += chunk;
}
return pa;
} else if (dataType == DataType.FLOAT) {
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*4);
FloatBuffer tbuff = bbuff.asFloatBuffer(); // typed buffer
float[] pa = new float[size];
while (index.hasNext()) {
bbuff.clear();
channel.read(bbuff, (long) beginOffset + index.next());
tbuff.position(0).limit(chunk);
tbuff.get(pa, offset, chunk); // copy from typed buffer into primitive array
offset += chunk;
}
return pa;
} else if (dataType == DataType.DOUBLE) {
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*8);
DoubleBuffer tbuff = bbuff.asDoubleBuffer(); // typed buffer
double[] pa = new double[size];
while (index.hasNext()) {
bbuff.clear();
channel.read(bbuff, (long) beginOffset + index.next());
tbuff.position(0).limit(chunk);
tbuff.get(pa, offset, chunk); // copy from typed buffer into primitive array
offset += chunk;
}
return pa;
}
throw new IllegalStateException();
}
public void create(String filename, ucar.nc2.NetcdfFile ncfile, boolean fill) throws IOException {
super.create( filename, ncfile, fill);
channel = raf.getChannel();
if (debug) out.println ("Opened file to write:'" + filename+ "', size=" + channel.size());
flush();
}
/**
* Write data subset to file for a variable, create primitive array.
* @param beginOffset: variable's beginning byte offset in file.
* @param index handles skipping around in the file.
* @param source from this buye buffer
* @param dataType dataType of the variable
* @return primitive array with data read in
*/
protected void writeData( Array aa, int beginOffset, Indexer index, DataType dataType) throws IOException {
int offset = 0;
int chunk = index.getChunkSize();
int size = index.getTotalSize();
if ((dataType == DataType.BYTE) || (dataType == DataType.CHAR)) {
byte[] pa;
if (dataType == DataType.BYTE)
pa = (byte[]) aa.getStorage();
else {
char[] cbuff = (char[]) aa.getStorage();
pa = convertCharToByte( cbuff);
}
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk);
while (index.hasNext()) {
bbuff.clear();
bbuff.put(pa, offset, chunk); // copy from primitive array to buffer
bbuff.flip();
channel.write(bbuff, beginOffset + index.next());
offset += chunk;
}
return;
} else if (dataType == DataType.SHORT) {
short[] pa = (short[]) aa.getStorage();
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*2);
ShortBuffer tbuff = bbuff.asShortBuffer(); // typed buffer
while (index.hasNext()) {
tbuff.clear();
tbuff.put(pa, offset, chunk); // copy from primitive array to typed buffer
bbuff.clear();
channel.write(bbuff, beginOffset + index.next());
offset += chunk;
}
return;
} else if (dataType == DataType.INT) {
int[] pa = (int[]) aa.getStorage();
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*4);
IntBuffer tbuff = bbuff.asIntBuffer(); // typed buffer
while (index.hasNext()) {
tbuff.clear();
tbuff.put(pa, offset, chunk); // copy from primitive array to typed buffer
bbuff.clear();
channel.write(bbuff, beginOffset + index.next());
offset += chunk;
}
return;
} else if (dataType == DataType.FLOAT) {
float[] pa = (float[]) aa.getStorage();
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*4);
FloatBuffer tbuff = bbuff.asFloatBuffer(); // typed buffer
while (index.hasNext()) {
tbuff.clear();
tbuff.put(pa, offset, chunk); // copy from primitive array to typed buffer
bbuff.clear();
channel.write(bbuff, beginOffset + index.next());
offset += chunk;
}
return;
} else if (dataType == DataType.DOUBLE) {
double[] pa = (double[]) aa.getStorage();
ByteBuffer bbuff = ByteBuffer.allocateDirect( chunk*8);
DoubleBuffer tbuff = bbuff.asDoubleBuffer(); // typed buffer
while (index.hasNext()) {
tbuff.clear();
tbuff.put(pa, offset, chunk); // copy from primitive array to typed buffer
bbuff.clear();
channel.write(bbuff, beginOffset + index.next());
offset += chunk;
}
return;
}
throw new IllegalStateException();
}
public void flush() throws IOException {
channel.force( false);
}
public void close() throws IOException {
if (!readonly) flush();
if (channel != null)
channel.close();
super.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy