Package com.stata.sfi
Class StrLConnector
java.lang.Object
com.stata.sfi.StrLConnector
- All Implemented Interfaces:
AutoCloseable
This class facilitates access to Stata's strL data type. All variable
and observation numbering begins at 1 unless otherwise stated.
Example:
This example reads a file and writes its contents into a single observation. The code could be modified to store data from other sources, such as from a network stream.
public static int dataWriteBytes(String args[]) {
String fileName = args[0];
byte[] bytes = null;
int rc;
if ((rc = Data.addVarStrL("mystrl")) !=0) return(rc);
if ((rc = Data.setObsTotal(1)) !=0) return(rc);
int mapv = Data.getVarIndex("mystrl");
long obs = 1;
File f = new File(fileName);
try (FileInputStream fin = new FileInputStream(f);
StrLConnector dsc = new StrLConnector(mapv, obs)) {
bytes = new byte[2048];
Data.allocateStrL(dsc, f.length());
// writes bytes in chunks
while (fin.read(bytes) != -1) {
Data.writeBytes(dsc, bytes, 0, bytes.length);
}
} catch (FileNotFoundException e) {
SFIToolkit.errorln(SFIToolkit.stackTraceToString(e));
return(601);
} catch (IOException e) {
SFIToolkit.errorln(SFIToolkit.stackTraceToString(e));
return (SFIToolkit.RC_GENERAL_ERROR);
} catch (Exception e) {
SFIToolkit.errorln(SFIToolkit.stackTraceToString(e));
return (SFIToolkit.RC_GENERAL_EXCEPTION);
}
return 0;
}
This example shows how to read a strL in binary form, writing each observation to a file.
public static int dataReadBytes(String args[]) {
long nobs1 = Data.getObsParsedIn1();
long nobs2 = Data.getObsParsedIn2();
byte[] bytes = new byte[1024 * 8];
String fileNameStub = args[0];
for (int var = 1; var <= Data.getParsedVarCount(); var++) {
int mapv = Data.mapParsedVarIndex(var);
for (long obs = nobs1; obs <= nobs2; obs++) {
if (!Data.isParsedIfTrue(obs))
continue;
File f = new File(fileNameStub + "_" + Data.getVarName(mapv) + "_"
+ obs + ".bin"); // make a filename for each var and obs
try (FileOutputStream fout = new FileOutputStream(f);
BufferedOutputStream bout = new BufferedOutputStream(fout);
StrLConnector dsc = new StrLConnector(mapv, obs)) {
int sz;
// read the bytes in chunks
while ((sz = Data.readBytes(dsc, bytes)) >= 0) {
bout.write(bytes, 0, sz);
}
} catch (FileNotFoundException e) {
SFIToolkit.errorln(SFIToolkit.stackTraceToString(e));
return(601);
} catch (IOException e) {
SFIToolkit.errorln(SFIToolkit.stackTraceToString(e));
return (SFIToolkit.RC_GENERAL_ERROR);
} catch (Exception e) {
SFIToolkit.errorln(SFIToolkit.stackTraceToString(e));
return (SFIToolkit.RC_GENERAL_EXCEPTION);
}
}
}
return 0;
}
-
Constructor Summary
ConstructorsConstructorDescriptionStrLConnector(int var, long obs) Creates a StrLConnector and connects it to a specific strL in the Stata dataset; seeData.StrLConnector(Frame frame, int var, long obs) Creates a StrLConnector and connects it to a specific strL in the specifiedFrame. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the connection and release any resources.longGet the current access position.longgetSize()Get the total number of bytes available in the strL.booleanisBinary()Determine if the attached strL has been marked as binary.voidreset()Reset the access position to its initial value.voidsetPosition(long pos) Set the access position.
-
Constructor Details
-
StrLConnector
Creates a StrLConnector and connects it to a specific strL in the specifiedFrame.- Parameters:
frame- TheFrameto reference.var- Variable to access.obs- Observation to access.- Throws:
IOException- Throws an IOException if an error occurs.
-
StrLConnector
Creates a StrLConnector and connects it to a specific strL in the Stata dataset; seeData.- Parameters:
var- Variable to access.obs- Observation to access.- Throws:
IOException- Throws an IOException if an error occurs.
-
-
Method Details
-
close
Close the connection and release any resources.- Specified by:
closein interfaceAutoCloseable
-
getPosition
public long getPosition()Get the current access position.- Returns:
- The position.
-
getSize
Get the total number of bytes available in the strL.- Returns:
- The total number of bytes available.
-
isBinary
Determine if the attached strL has been marked as binary.- Returns:
- True if the strL has been marked as binary.
- Throws:
IOException- Throws an IOException if an error occurs.
-
reset
Reset the access position to its initial value. -
setPosition
Set the access position.- Parameters:
pos- The new position.
-