Package com.stata.sfi

Class StrLConnector

java.lang.Object
com.stata.sfi.StrLConnector
All Implemented Interfaces:
AutoCloseable

public class StrLConnector extends Object implements 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

    Constructors
    Constructor
    Description
    StrLConnector(int var, long obs)
    Creates a StrLConnector and connects it to a specific strL in the Stata dataset; see Data.
    StrLConnector(Frame frame, int var, long obs)
    Creates a StrLConnector and connects it to a specific strL in the specified Frame.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the connection and release any resources.
    long
    Get the current access position.
    long
    Get the total number of bytes available in the strL.
    boolean
    Determine if the attached strL has been marked as binary.
    void
    Reset the access position to its initial value.
    void
    setPosition(long pos)
    Set the access position.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StrLConnector

      public StrLConnector(Frame frame, int var, long obs) throws IOException
      Creates a StrLConnector and connects it to a specific strL in the specified Frame.
      Parameters:
      frame - The Frame to reference.
      var - Variable to access.
      obs - Observation to access.
      Throws:
      IOException - Throws an IOException if an error occurs.
    • StrLConnector

      public StrLConnector(int var, long obs) throws IOException
      Creates a StrLConnector and connects it to a specific strL in the Stata dataset; see Data.
      Parameters:
      var - Variable to access.
      obs - Observation to access.
      Throws:
      IOException - Throws an IOException if an error occurs.
  • Method Details

    • close

      @Synchronized public void close()
      Close the connection and release any resources.
      Specified by:
      close in interface AutoCloseable
    • getPosition

      public long getPosition()
      Get the current access position.
      Returns:
      The position.
    • getSize

      @Synchronized public long getSize()
      Get the total number of bytes available in the strL.
      Returns:
      The total number of bytes available.
    • isBinary

      @Synchronized public boolean isBinary() throws IOException
      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

      @Synchronized public void reset()
      Reset the access position to its initial value.
    • setPosition

      @Synchronized public void setPosition(long pos)
      Set the access position.
      Parameters:
      pos - The new position.