public class StrLConnector extends Object implements AutoCloseable
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 and Description |
---|
StrLConnector(Frame frame,
int var,
long obs)
Creates a StrLConnector and connects it to a specific strL in the
specified
Frame . |
StrLConnector(int var,
long obs)
Creates a StrLConnector and connects it to a specific strL in the
Stata dataset; see
Data . |
StrLConnector(String rname,
int var,
long obs)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the connection and release any resources.
|
long |
getPosition()
Get the current access position.
|
long |
getSize()
Get the total number of bytes available in the strL.
|
boolean |
isBinary()
Determine if the attached strL has been marked as binary.
|
void |
reset()
Reset the access position to its initial value.
|
void |
setPosition(long pos)
Set the access position.
|
public StrLConnector(Frame frame, int var, long obs) throws IOException
Frame
.frame
- The Frame
to reference.var
- Variable to access.obs
- Observation to access.IOException
- Throws an IOException if an error occurs.public StrLConnector(int var, long obs) throws IOException
Data
.var
- Variable to access.obs
- Observation to access.IOException
- Throws an IOException if an error occurs.@Deprecated public StrLConnector(String rname, int var, long obs) throws IOException
DataFrame
.rname
- Reference name for the data frame.var
- Variable to access.obs
- Observation to access.IOException
- Throws an IOException if an error occurs.@Synchronized public void close()
close
in interface AutoCloseable
public long getPosition()
@Synchronized public long getSize()
@Synchronized public boolean isBinary() throws IOException
IOException
- Throws an IOException if an error occurs.@Synchronized public void reset()
@Synchronized public void setPosition(long pos)
pos
- The new position.